【问题标题】:Free 2d array of char with realloc [closed]使用 realloc 释放 char 的 2d 数组 [关闭]
【发布时间】:2016-08-24 13:37:49
【问题描述】:

在以下代码中,当我尝试释放 2D 字符数组(即 2dArray[l])的各个元素时,出现错误(_crtisvalidheappointer(puserdata) dbgheap.c): 关于错误原因的任何想法?

#include<stdio.h>
int main(){    
    char ** TwodArray= NULL;
    int k = 0;
    int j = 0;

    do{
        k++;
        for (int i= 0; i<10; i++){
            j++;
            TwodArray=(char**)realloc(TwodArray, (j+1)*10*sizeof(char*));
            TwodArray[j-1]=(char*)malloc(10 *sizeof(char));
            TwodArray[j-1] = "abcdefgh";
            ....
        }        
    }while(k<3)
    // free the memory
    for (int l = 0; l < j; l++){
        if (TwodArray[l] != NULL)
            free(TwodArray[l]); // here I get the error!
    }
    free(TwodArray);
    return 0;
}

【问题讨论】:

  • 请给我们看一些真实的代码。
  • @JoachimPileborg 真正的代码很复杂很长,我这里尽量简化了!没有任何编译错误,而是我在帖子中提到的运行时错误:'_crtisvalidheapppointer(puserdata) dbgheap.c
  • 好吧@Homer,问题是这段代码甚至不是c。 2dArray 是无效的参数名称。无论如何,在修复缺少的包括后,;让你的 do while 循环和参数名称起作用——这个 sn-p 有效。
  • 你的代码中有 no 二维数组,注意它可以代表一个也不能指向一个。指针不是数组。释放 2D 数组就像 free(my2Darray); 一样简单。
  • @IshayPeled 现在它坏了!我在代码中添加了TwodArray[j-1] = "abcdefgh";

标签: c arrays free realloc


【解决方案1】:

您正在尝试释放静态字符串。

如果你写类似

char* a = "hello";

a 没有指向动态内存区域。它是静态的,所以你不能释放它。

【讨论】:

    【解决方案2】:
    TwodArray[j-1] = "abcdefgh";
    

    紧随其后

    free(TwodArray[l]);   // with l = j -1 from above  
    

    会失败,因为TwodArray[j-1] 的值没有指向malloc 分配的东西

    你想做吗:

    strcpy(TwodArray[j-1], "abcdefgh");
    

    解释是TwodArray[j-1] = "abcdefgh"; 改变了指针TwodArray[j-1] 的值,而strcpy(TwodArray[j-1], "abcdefgh"); 将字符串"abcdefgh" 复制到TwodArray[j-1] 指向的位置(并且保持TwodArray[j-1] 的值不变)。

    失败代码示例

    #include<stdio.h>
    #include<stdlib.h>
    int main (void)
    {
      char* p = malloc(10 * sizeof(char));
      printf("%p\n", (void*)p);
      p = "aaaa";                  // Bad....
      printf("%p\n", (void*)p);
      free(p);
      return 1;
    }
    

    可能的输出

    0x1b76010

    0x400688

    * `./a.out' 中的错误:free():无效指针:0x0000000000400688 *

    中止

    工作代码示例

    #include<stdio.h>
    #include<stdlib.h>
    #include <string.h>
    
    int main (void)
    {
      char* p = malloc(10 * sizeof(char));
      printf("%p\n", (void*)p);
      strcpy(p, "aaaa");
      printf("%p\n", (void*)p);
      printf("%s\n", p);
      free(p);
      return 1;
    }
    

    可能的输出

    0x1d2c010

    0x1d2c010

    啊啊

    【讨论】:

      【解决方案3】:

      简而言之,不要释放静态字符串:

      char *a = "aaaa";
      free(a)
      

      会导致同样的错误。

      这将起作用:

      #include <string.h>
      #include <stdlib.h>
      #include<stdio.h>
      
      int main(){
          char ** TwodArray= NULL;
          int k = 0;
          int j = 0;
      
          do{
              k++;
              for (int i= 0; i<10; i++){
                  j++;
                  TwodArray=(char**)realloc(TwodArray, (j+1)*10*sizeof(char*));
                  TwodArray[j-1] = strndup("abcdefgh",8);
              }
          }while(k<3);
          // free the memory
          for (int l = 0; l < j; l++){
              if (TwodArray[l] != NULL)
                  free(TwodArray[l]); // here I get the error!
          }
          free(TwodArray);
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-17
        • 1970-01-01
        • 2016-03-27
        • 2014-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-17
        相关资源
        最近更新 更多