【问题标题】:How to use realloc in C [duplicate]如何在C中使用realloc [重复]
【发布时间】:2020-11-25 12:28:28
【问题描述】:

我正在尝试使用 realloc 函数重新分配内存,我看到您之前需要使用 malloc,但我不明白您是否必须使用它,因为假设我正在创建以下字符串:

char string[] =  "fun";

如果我尝试添加更多空间,realloc 函数会起作用吗?

这让我想到了我的问题,我试图简单地在字符串的末尾添加一个字母,让我们说“p”,但由于某种原因,程序每次运行时都会在 realloc 行上崩溃。

这是我的完整代码:

int main()
{
char string[] =  "fun" ;
str_func(string);
printf("%s", string);
return 0;
} 

void str_func(char* str)
{
str = (char*)realloc(str, strlen(str) + 2);
strcat(str, "p");
}

我还尝试创建一个指向“字符串”的指针并发送该指针,结果相同。

【问题讨论】:

  • 不,您只能通过realloc() 获得来自malloc()calloc()(或来自以下realloc())或NULL 的东西。
  • 所以你必须使用 malloc() 之前?
  • realloc(NULL, 42) 是可以的,这种情况之前不需要使用malloc()
  • 是的。不要混合使用堆栈分配的数组(普通数组)和堆分配的数组(mallocrealloc)。
  • 这能回答你的问题吗? C: Expanding an array with malloc

标签: c realloc


【解决方案1】:

如果我尝试添加更多空间,realloc 函数会起作用吗?

不,因为该数组没有在堆上分配 - 在您的情况下,它很可能在堆栈上分配并且无法调整大小。简而言之:realloc 无法识别指针,也不知道如何处理它,但无论如何都会尝试做某事,从而导致崩溃。

您只能在先前传递给malloc 的指针或空指针上调用realloc。这就是这些函数的工作原理。

详情请见What gets allocated on the stack and the heap?

【讨论】:

    【解决方案2】:

    我之前看到你需要使用 malloc,但我不明白你是否必须使用它

    如果您需要在使用realloc 之前使用malloc,那么根据定义,您必须只使用realloc 最初分配给malloc 的东西。

    你试图在“需要”和“必须”之间找到一些不存在的空间。

    ...由于某种原因,程序在 realloc 上崩溃了

    你已经说过你知道你需要使用malloc。然后你没有使用malloc,你问为什么这是个问题。你至少可以尝试做你“知道”你需要做的事情,看看是否能解决问题。

    程序应该看起来像

    int main()
    {
      /* array is an automatic local variable. It wasn't dynamically allocated
         in the first place, so can't be dynamically re-allocated either.
         You cannot (and don't need to) free it either, it just goes out of scope
         like any other automatic variable.
      */
      char array[] = "fun";
    
      /* you need to use malloc (or one of the other dynamic allocation functions)
         before you can realloc, as you said yourself */
      char *dynamic = malloc(1+strlen(array));
      memcpy(dynamic, array, 1+strlen(array));
    
      /* realloc can move your data, so you must use the returned address */
      dynamic = str_func(dynamic);
      printf("old:'%s', new:'%s'\n", array, dynamic);
    
      /* not really essential since the program is about to exit anyway */
      free(dynamic);
    } 
    
    char* str_func(char* str)
    {
      char* newstr = realloc(str, strlen(str) + 2);
      if (newstr) {
        strcat(newstr, "p");
        return newstr;
      } else {
        /* we failed to make str larger, but it is still there and should be freed */
        return str;
      }
    }
    

    你原来的条件不太正确:实际上是传递给realloc的指针

    ...必须由 malloc()calloc()realloc() 先前分配,并且尚未通过调用 free 或 realloc 释放

    [OR] 如果 ptr 为 NULL,则行为与调用 malloc(new_size) 相同。

    【讨论】:

      【解决方案3】:

      realloc 函数仅适用于最初使用一小组分配函数(例如 malloccallocrealloc 本身)或空指针创建的事物。由于string 不是这些东西,因此您的代码定义不明确。

      【讨论】:

        猜你喜欢
        • 2018-11-24
        • 2013-04-16
        • 1970-01-01
        • 2012-01-09
        • 2011-05-04
        • 1970-01-01
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        相关资源
        最近更新 更多