问题描述
我的内存优化的code我有一个嵌入式应用。它的工作原理很好,但这样做的结果是,我得到了很多1D,2D和3D mallocs,并在减慢执行时间的函数中间释放。
I memory optimized a code I have for embedded use. It works well but the result of this is that I got a lot of 1D, 2D and 3D mallocs and frees in the middle of functions that slow down the execution time.
由于多种原因,我决定修改我在做它的方式。我想在我的执行开始跟单的malloc分配所有的内存,我可以和刚点,其中每个阵列需要在正确的存储空间。
For several reasons, I decided to modify the way I was doing it. I want to allocate all the memory I can with a single malloc at the start of my execution and just point the correct memory space where each array needs to be.
有关信息,我在x86现在,所以我没有任何的存储空间问题执行此。我宣布我的数组是这样的:
For info, I execute this on x86 for now so I don't any memory space issues. I declare my arrays this way:
unsigned char *memory;
memory = (unsigned char *)malloc(MYSIZE*sizeof(unsigned char));
type* p1;
p1 = (type *)((int)memory + p1_offset);
type** p2;
p2 = (type **)((int)memory + p2_offset);
for (int i=0 ; i<p2_height ; i++)
{
p2[i] = (type *)((int)p2 + p2_width*sizeof(type));
}
虽然它很适合我的一维指针,它返回我为我的2D指针声明段错误。我检查了我的补偿,他们是比较不错的内存指针。因为我没有经历过与我的声明也许指针我在这里误解的东西,所以我会很高兴,如果有人能更解释我这个技术的这种方式!
While it works well for my 1D pointer, it returns me a segfault for my 2D pointer declaration. I checked my offsets and they are good compare to the memory pointer. As I'm not experienced with this way of declaring my pointers maybe I'm misunderstanding something here so I would be pleased if someone can explain me more about this technique !
推荐答案
您正在声明P2作为指针的指针数组,而不是一个指针到一个平面二维数组。你还(编辑为清晰起见:)初始化 P2
用垃圾整数,则铸造回一个指针解引用它。
You’re declaring p2 as a pointer to an array of pointers, not a pointer to a flat two-dimensional array. You’re also (edited for clarity:) initializing p2
with a garbage integer, then casting it back to a pointer and dereferencing it.
编辑补充例如code:
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
/* Boilerplate to turn this into a MWE: */
#define MYSIZE 1024U
typedef double elem_t;
static const size_t p1_offset = 0, p2_offset = 512;
/* Our buffer will hold W 1d elements and X*Y 2d elements. */
#define W 64U
#define X 32U
#define Y 2U
typedef struct {
elem_t array1[W];
elem_t array2[X][Y];
} spaces_t;
/* Test driver: */
int main(void)
{
/* sizeof(unsigned char) is defined as 1. Do you mean to allocate an
* array of MYSIZE bytes or MYSIZE elements of type?
*/
spaces_t * const memory = malloc(sizeof(spaces_t));
if (!memory) {
perror("malloc");
exit(EXIT_FAILURE);
}
elem_t* p1 = memory->array1;
elem_t* p2 = (elem_t*)(memory->array2);
/* Never cast a pointer to int. It's not even legal.
* Why does this assertion succeed? Why are memory and bad_idea
* equal, but memory+1 and bad_idea+1 different by the size of both
* of our arrays combined, minus one byte?
*/
const uintptr_t bad_idea = (uintptr_t)memory;
assert( (uintptr_t)(memory+1) - (bad_idea+1) == sizeof(spaces_t) - 1 );
/* Let’s initialize all the arrays. No segfaults? */
size_t i,j;
for (i = 0; i < W; ++i) {
*p1 = (elem_t)i;
assert( memory->array1[i] == *p1 );
++p1;
}
/* This is a lot faster when X is a power of 2: */
for (i = 0; i < X; ++i)
for ( j = 0; j < Y; ++j) {
*p2 = (elem_t)(100*i + j);
assert( memory->array2[i][j] == *p2 );
++p2;
}
return EXIT_SUCCESS;
}
这篇关于如何在2D / 3D空间中的点上pre-分配的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!