重新分配连续的2D数组

我使用Shawn Chin在这里发布的方法生成连续的2d数组。[1] [2] 它工作得很好。

简要地从他的post:

char** allocate2Dchar(int count_x, int count_y) { int i; # allocate space for actual data char *data = malloc(sizeof(char) * count_x * count_y); # create array or pointers to first elem in each 2D row char **ptr_array = malloc(sizeof(char*) * count_x); for (i = 0; i < count_x; i++) { ptr_array[i] = data + (i*count_y); } return ptr_array; } 

以下免费function:

 void free2Dchar(char** ptr_array) { if (!ptr_array) return; if (ptr_array[0]) free(ptr_array[0]); free(ptr_array); } 

对于我来说,如何在任一维度中创建等效的重新分配函数并不是很明显,尽管我只对在保持连续性的同时重新分配行数感兴趣。 增加列数会很有趣,但可能很难理解。 我没有找到任何关于这个问题的直接讨论,除了说“这很难!”。[2]

当然,这可以通过一种可怕的暴力方法来实现,将数据复制到新的一维数组(上面的数据)进行存储,重新分配一维数组,然后释放指针(ptr_array)并将其重新生成行元素。新尺寸。 但是,对于行修改来说这很慢,因为有必要至少将内存需求翻倍以复制数据,这对于更改列数确实非常糟糕。

这是用于改变行数的所述方法的示例(由于指针的偏移对于数据而言是错误的,因此不能正确地改变列的数量)。 我还没有完全测试过这个,但你明白了……

 double ** reallocate_double_array (double **ptr_array, int count_row_old, int count_row_new, int count_col) { int i; int old_size = count_row_old * count_col; int new_size = count_row_new * count_col; double *data = malloc (old_size * sizeof (double)); memcpy (&data[0], &ptr_array[0][0], old_size * sizeof (double)); data = realloc (data, new_size * sizeof (double)); free (ptr_array[0]); free (ptr_array); ptr_array = malloc (count_row_new, sizeof (double *)); for (i = 0; i < count_row_new; i++) ptr_array[i] = data + (i * count_col); return ptr_array; } 

另外,这种方法要求你知道以前的尺寸,这是令人讨厌的!

任何想法都非常感激。

[1] 如何使用双指针分配2D数组?

[2] http://www.eng.cam.ac.uk/help/tpl/languages/C/teaching_C/node52.html

第一个malloc和memcpy是不必要的,因为您可以轻松访问ptr_array[0]的原始数据数组。 您不需要知道旧的大小,因为realloc应该调用它在地址分配的数量并移动正确的数据量。

这样的事情应该有效。

 double ** reallocate_double_array (double **ptr_array, int count_row_new, int count_col) { int i; int new_size = count_row_new * count_col; double *data = ptr_array[0]; data = realloc (data, new_size * sizeof (double)); free (ptr_array); ptr_array = calloc (count_row_new, sizeof (double *)); for (i = 0; i < count_row_new; i++) ptr_array[i] = data + (i * count_col); return ptr_array; }