2010-02-12 68 views
1

我想在一个函数调用中创建一个连续的内存块,该函数调用将第一部分内存作为指针数组指向其他块。基本上,我试图做到:与malloc连续内存块

int **CreateInt2D(size_t rows, size_t cols) 
{ 
    int **p, **p1, **end; 
    p = (int **)SafeMalloc(rows * sizeof(int *)); 
    cols *= sizeof(int); 
    for (end = p + rows, p1 = p; p1 < end; ++p1) 
     *p1 = (int *)SafeMalloc(cols); 
    return(p); 
} 

void *SafeMalloc(size_t size) 
{ 
    void *vp; 

    if ((vp = malloc(size)) == NULL) { 
     fputs("Out of mem", stderr); 
     exit(EXIT_FAILURE); 
    } 
    return(vp); 
} 

但有一个块。这是据我已经得到了:

int *Create2D(size_t rows, size_t cols) { 
int **memBlock; 
int **arrayPtr; 
int loopCount; 
    memBlock = (int **)malloc(rows * sizeof(int *) + rows * cols * sizeof(int)); 
    if (arrayPtr == NULL) { 
     printf("Failed to allocate space, exiting..."); 
     exit(EXIT_FAILURE); 
    } 
    for (loopCount = 1; loopCount <= (int)rows; loopCount++) { 
     arrayPtr = memBlock + (loopCount * sizeof(int *)); 
     //I don't think this part is right. do I need something like arrayPtr[loopCount] = .... 
    } 
return(memBlock); 
} 

回答

1

像这样的东西

int **Create2D(size_t rows, size_t cols) 
{ 
    size_t cb = (rows * sizeof(int *)) + (rows * cols * sizeof(int)); 
    int * pmem = (int *)SafeMalloc(cb); 

    int ** prows = (int **)pmem; 
    int * pcol = (int *)&prows[rows]; // point pcol after the last row pointer 

    for (int ii = 0; ii < rows; ++ii) 
    { 
     prows[ii] = pcol; 
     pcol += cols; 
    } 

    return prows; 
} 
+0

我想我得到你的答案,但我在为行指针和列分配内存的想法有问题。因此,对于那块内存,cb,你可以只是将pmem部分分类来分割它?像第一部分是指向指针的指针,然后指向最后一行指针的pcol,然后初始化cols并让指针指向的指针指向pcol?像那样的东西? – Crystal 2010-02-12 09:50:32

+1

是的。你分配总数,然后使用指针数学得到一个pcol指针,指向第一个地址_after_你想要分配给prows的部分,然后遍历pcol指针并使用它来初始化prow数组。最终的结果是一个分配,但与原始代码具有相同的布局。 – 2010-02-12 09:55:21

+0

@Crystal:再次检查代码,我有一个循环中的错误。抱歉。 – 2010-02-12 10:01:07

0

我不太清楚你想要做什么,而是你的代码的最后一块是马车。您针对NULL测试arrayPtr,但从不分配它。在您分配给arrayPtr的for()循环中,但实际上并没有对它做任何事情。

如果您正在寻找使用一个单独的内存块,然后有什么错一个二维数组:

int* array = (int*)malloc(rows * count * sizeof(int)); 
int* someCellPtr = &array[y * rows + x]; 

0

,如果你想二维数组与一个页头,您可以使用calloc

int** p2DArray = (int**)calloc(rows,cols * sizeof(int)); 

或只是malloc的:

int** p2DArray = (int**)malloc(rows * cols * sizeof(int)); 

这使正常的索引:

​​
1

看来你并没有清楚的描述你想要什么 来实现。 记录它!它会清除你的思想,除此之外,如果你不明白它,没有人会和 这样的代码是一个噩梦来维持(即使时间 通过时,甚至适用于你自己)。

要创建一个分配连续内存块的函数,必须调用SafeMalloc一次,并使用一次将使用的内存总量。

/* 
* Memory layout example for 2 rows and 3 cols 
* 
*      1 1 1 1 1 1 1 1 1 1 
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
* |P|P|C|C|C|C|C|C|C|C|C|C|C|C|C|C|C|C|C|C| 
* |1|2|1|1|1|2|2|2|3|3|3|1|1|1|2|2|2|3|3|3| 
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
* 
* P1 is a pointer coloum data for row 1, points to memory start + offset 2 (assuming sizeof(int) == sizeof(int *)) 
* P2 is for row 2, points to memory start + offset 11 
* C1 is coloumn 1 data, etc 
*/ 
int **CreateInt2D(size_t rows, size_t cols) 
{ 
     int **memory_start, **p1, *col_data; 
     size_t total_memory_to_allocate; 

     total_memory_to_allocate = rows * sizeof(int *) + rows * cols * sizeof(int); 
     memory_start = (int **) SafeMalloc(total_memory_to_allocate); 

     for (col_data = (int *)(memory_start + rows), p1 = memory_start; 
      p1 < (int **)col_data; 
      ++p1, col_data += cols * sizeof(int)) 
       *p1 = col_data; 

     return memory_start; 
} 

这个例子是基于尽可能地贴近你的原始越好,约翰Knoeller的答案通过阵列认购大概是这样做的更好的方法。