连续二维数组的重新分配 [英] Reallocation of contiguous 2D array

查看:26
本文介绍了连续二维数组的重新分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Shawn Chin 在此处发布的方法生成连续的二维数组.[1][2]效果很好.

I am generating contiguous 2d arrays using the method posted on here by Shawn Chin.[1][2] It works very well.

简要摘自他的帖子:

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;
}

以及以下免费功能:

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

我不清楚如何在任一维度中创建等效的重新分配函数,尽管我只对在保持连续性的同时重新分配行数感兴趣.增加列数会很有趣,但可能相当困难.除了说这很难!"之外,我没有找到任何直接讨论这个问题的地方.[2]

It is not obvious to me how to create an equivalent reallocate function in either dimension, though I am only interested in realloc'ing the number of rows while maintaining continuity. Growing the number of columns would be interesting to understand but probably quite difficult. I haven't found any direct discussion of this issue anywhere other than to say, "it's hard!".[2]

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

Of course this is doable by a horrible brute force method, copying the data to a new 1D array (data, above) for storage, realloc'ing the 1D array, then freeing and regenerating the pointers (ptr_array) to the row elements for the new size. This, however, is pretty slow for row modifications, since it is necessary to at least double the memory requirement to copy out the data, and this is truly horribly bad for changing the number of columns.

这是用于更改行数的上述方法的示例(更改列数将无法正常工作,因为指针的偏移量对于数据而言是错误的).我还没有完全测试过这个,但你明白了......

This is an example of said method for changing the number of rows (it wouldn't work properly for changing the number of columns because the offsets for the pointers would be wrong for the data). I haven't fully tested this, but you get the idea ...

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;
}

另外,这个方法需要你知道之前的尺寸,这很讨厌!

Plus, this method requires you know the previous size, which is obnoxious!

非常感谢任何想法.

[1] 如何使用双指针分配二维数组?

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

推荐答案

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

The first malloc and the memcpy are unnecessary, because you have easy access to the original data array at ptr_array[0]. You don't need to know the old size, because realloc should recall how much it allocated at the address and move the correct ammount of data.

这样的事情应该可以工作.

Something like this should work.

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;
}

这篇关于连续二维数组的重新分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆