【问题标题】:Realloc index of array数组的realloc索引
【发布时间】:2012-09-29 20:20:43
【问题描述】:

有一个数组:

type **a;

a[0][data_0]
a[1][data_1]
a[2][data_2]
a[n][data_n]

当扩展这样一个数组时:

  1. realloc()asizeof(type*) * (n + 1)
  2. malloc() (*a[n]) 适合 data_n 其中data_n 的大小可变。

a 中的realloc() 会不会有问题?

a[2] 将始终指向 data_2,即使 a 在内存中移动,或者该链接是否会丢失?

据我了解,我最终会在内存中得到类似的东西:

         Address               Address
a[0] => 0x###131, (*a[0]) => 0x####784
a[1] => 0x###135, (*a[1]) => 0x####793
a[2] => 0x###139, (*a[2]) => 0x####814

realloc() 之后,我可能会得到类似的结果:

         Address               Address
a[0] => 0x###216, (*a[0]) => 0x####784
a[1] => 0x###21a, (*a[1]) => 0x####793
a[2] => 0x###21e, (*a[2]) => 0x####814
a[n] => 0x###zzz, (*a[n]) => 0x####yyy

这是正确的吗? data_n 段保持不变,或者它们也可以移动?

【问题讨论】:

    标签: c arrays malloc realloc


    【解决方案1】:

    这是正确的吗? data_n 段保持不变,或者它们也可以移动?

    是的,0 <= i < na[i] 的值被复制到新位置,因此指针指向相同的 data_i,并且不会移动。

    realloc()a 会不会有问题?

    当然,realloc 总是会失败并返回NULL,所以你不应该这样做

    a = realloc(a, new_size);
    

    但是使用一个临时变量来存储realloc的返回值。

    【讨论】:

    • 谢谢。必须百分百确定。同样,如果我有一个单独的指向 data_nn 的 ptr,它在增长 a 后不会被破坏。 (我在链接列表上通过realloc() 做了一个嘘声 - 不聪明。)
    • 对,不会损坏。 realloc只知道当前块的地址和大小,以及请求的新大小,它不知道那里存储了什么样的数据,所以如果它移动块,它只是复制数据(可能使用@987654332 @)。
    【解决方案2】:

    如果您要重新分配到更大的大小,则原始字节不会改变。

    例子:

    char **a = malloc(n*sizeof(char *));
    for (i=0; i!=n; ++i) {
      a[i] = malloc(m);
    }
    
    a = realloc(a,(n+1)*sizeof(char *));
    // a[0]...a[n-1] are still the same
    a[n] = malloc(m);
    

    【讨论】:

      【解决方案3】:

      是的,即使在 realloc() 之后,旧值的地址仍然保持不变

      你可以在这个程序的输出中看到:

      shubhanshm@BANLSHUBHANSH /cygdrive/f/My Codes/Practice/C
      $ ./a.exe
      Original Array details before using realloc():
      Printing array with size 4x4
      1       0       0       19
      2       4       0       19
      3       6       9       27
      4       8       12      16
      Printing array addresses with size 4x4
      a[0][0]=1       ->0x20010260
      a[0][1]=0       ->0x20010264
      a[0][2]=0       ->0x20010268
      a[0][3]=19      ->0x2001026c
      
      a[1][0]=2       ->0x20010270
      a[1][1]=4       ->0x20010274
      a[1][2]=0       ->0x20010278
      a[1][3]=19      ->0x2001027c
      
      a[2][0]=3       ->0x20010280
      a[2][1]=6       ->0x20010284
      a[2][2]=9       ->0x20010288
      a[2][3]=27      ->0x2001028c
      
      a[3][0]=4       ->0x20010290
      a[3][1]=8       ->0x20010294
      a[3][2]=12      ->0x20010298
      a[3][3]=16      ->0x2001029c
      
      Array details after using realloc():
      Printing array with size 5x4
      1       0       0       19
      2       4       0       19
      3       6       9       27
      4       8       12      16
      5       10      15      20
      Printing array addresses with size 5x4
      a[0][0]=1       ->0x20010260
      a[0][1]=0       ->0x20010264
      a[0][2]=0       ->0x20010268
      a[0][3]=19      ->0x2001026c
      
      a[1][0]=2       ->0x20010270
      a[1][1]=4       ->0x20010274
      a[1][2]=0       ->0x20010278
      a[1][3]=19      ->0x2001027c
      
      a[2][0]=3       ->0x20010280
      a[2][1]=6       ->0x20010284
      a[2][2]=9       ->0x20010288
      a[2][3]=27      ->0x2001028c
      
      a[3][0]=4       ->0x20010290
      a[3][1]=8       ->0x20010294
      a[3][2]=12      ->0x20010298
      a[3][3]=16      ->0x2001029c
      
      a[4][0]=5       ->0x20048300
      a[4][1]=10      ->0x20048304
      a[4][2]=15      ->0x20048308
      a[4][3]=20      ->0x2004830c
      

      以上输出的源代码为:

      #include<stdio.h>
      #include<stdlib.h>
      
      #define mul(x, y) (((x)+1)*((y)+1))
      
      void printArr(int **a, int r, int c){
          int i, j;
          printf("Printing array with size %dx%d\n", r, c );
          for(i = 0; i < r; i++){
              for(j = 0; j < c; j++){
                  printf("%d\t", a[i][j]);
              }
              printf("\n");
          }
      }
      
      void printArrAddress(int **a, int r, int c){
          int i, j;
          printf("Printing array addresses with size %dx%d\n", r, c );
          for(i = 0; i < r; i++){
              for(j = 0; j < c; j++){
                  printf("a[%d][%d]=%d\t->%p\n", i, j, a[i][j], &a[i][j]);
              }
              printf("\n");
          }
      }
      
      int main(){
          int **a;
          int n = 4;
          a = (int**)malloc(sizeof(int*)*n);
          int i, j;
          for(i = 0; i< n; i++){
              a[i] = (int*)malloc(sizeof(int)*(i+1));
              for(j = 0; j < i+1; j++ )a[i][j] = mul(i, j);
          }
          printf("Original Array details before using realloc():\n");
          printArr(a, n, n);
          printArrAddress(a, n, n);
      
          a = (int**)realloc(a, sizeof(int*)*(n+1));
          a[n] = (int*)malloc(sizeof(int)*n);
          for( i = 0; i< n; i++){
              a[n][i] = mul(n, i);
          }
          printf("Array details after using realloc():\n");
          printArr(a, n+1, n);
          printArrAddress(a, n+1, n);
          return 0;
      }
      

      我希望这能澄清一些事情。

      【讨论】:

        猜你喜欢
        • 2018-08-22
        • 2018-10-09
        • 1970-01-01
        • 2018-08-29
        • 1970-01-01
        • 2017-07-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多