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

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

问题描述

我生成使用贴在这里由肖恩下巴的方法,连续的二维阵列。[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);
}

这不是明摆着要我如何可以维度创建一个等效REALLOCATE功能,我只关心realloc'ing行的数量,同时保持连续性,但。成长中的列数将是有趣的理解,但恐怕相当困难。我还没有发现这个问题的任何地方,只是说,很难!任何直接的讨论。[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]

当然,这是可行的由一个可怕蛮力的方法,该数据复制到新的一维阵列(数据段)进行存储,realloc'ing一维阵列,然后释放和再生指针(ptr_array)到行元件为新的大小。然而,这是用于行修改pretty的速度慢,这是因为需要至少两倍的存储器要求来复制出的数据,这是真正的可怕坏用于改变的列数

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

此外,这种方法需要您知道previous大小,这是很厉害的!

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

任何想法大大AP preciated。

Any thoughts greatly appreciated.

[1] <一个href="http://stackoverflow.com/questions/5040496/how-can-i-allocate-a-2d-array-using-double-pointers?tab=votes#tab-top">How采用双指针我可以分配一个二维数组?

[2] 的http:// WWW .eng.cam.ac.uk /帮助/ TPL /语言/ C / teaching_C / node52.html

推荐答案

第一的malloc和memcpy的是不必要的,因为你可以很方便地访问原始数据数组 ptr_array [0] 。你不需要知道旧的大小,因为realloc的应收回该产品在地址多少分配和移动数据的正确ammount的。

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.

这样的事情应该工作。

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天全站免登陆