C语言 如何在C中的函数中使用realloc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13748338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:41:47  来源:igfitidea点击:

How to use realloc in a function in C

carraysmalloc

提问by Legendre

Building on what I learned here: Manipulating dynamic array through functions in C.

以我在这里学到的知识为基础:Manipating dynamic array through functions in C

void test(int data[])
{
    data[0] = 1;    
}    

int main(void)
{    
    int *data = malloc(4 * sizeof *data);

    test(data);

    return 0;
}

This works fine. However, I am also trying to using reallocin a function.

这工作正常。但是,我也尝试realloc在函数中使用。

void increase(int data[])
{
    data = realloc(data, 5 * sizeof *data);    
}

This complies but the program crashes when run.

这符合但程序在运行时崩溃。

Question

How should I be using realloc in a function?

我应该如何在函数中使用 realloc ?

I understand that I should assign the result of reallocto a variable and check if it is NULLfirst. This is just a simplified example.

我知道我应该将 的结果分配realloc给一个变量并检查它是否是NULL第一个。这只是一个简化的例子。

采纳答案by simonc

You want to modify the value of an int*(your array) so need to pass a pointer to it into your increasefunction:

您想修改一个int*(您的数组)的值,因此需要将指向它的指针传递给您的increase函数:

void increase(int** data)
{
    *data = realloc(*data, 5 * sizeof int);
}

Calling code would then look like:

调用代码将如下所示:

int *data = malloc(4 * sizeof *data);
/* do stuff with data */
increase(&data);
/* more stuff */
free(data);

回答by Ramy Al Zuhouri

Keep in mind the difference between a pointer and an array.
An array is a chuck of memory in the stack, and that's all.If you have an array:

请记住指针和数组之间的区别。
数组是堆栈中的一块内存,仅此而已。如果您有一个数组:

int arr[100];

Then arr is an address of memory, but also &arr is an adress of memory, and that address of memory is constant, not stored in any location.So you cannot say arr=NULL, since arr is not a variable that points to something.It's just a symbolic address: the address of where the array starts.Instead a pointer has it's own memory and can point to memory addresses.

那么 arr 是内存地址,而且 &arr 也是内存地址,内存地址是常量,不存储在任何位置。所以你不能说 arr=NULL,因为 arr 不是一个指向某物的变量。它只是一个符号地址:数组开始的地址。相反,指针拥有自己的内存并且可以指向内存地址。

It's enough that you change int[] to int*.
Also, variables are passed by copy so you need to pass an int** to the function.

将 int[] 更改为 int* 就足够了。
此外,变量通过复制传递,因此您需要将 int** 传递给函数。

About how using realloc, all the didactic examples include this:

关于如何使用 realloc,所有的教学示例都包括:

  1. Use realloc;
  2. Check if it's NULL.In this case use perror and exit the program;
  3. If it's not NULL use the memory allocated;
  4. Free the memory when you don't need it anymore.
  1. 使用重新分配;
  2. 检查是否为NULL。在这种情况下使用perror并退出程序;
  3. 如果不是 NULL 则使用分配的内存;
  4. 当您不再需要时释放内存。

So that would be a nice example:

所以这将是一个很好的例子:

int* chuck= (int*) realloc (NULL, 10*sizeof(int)); // Acts like malloc,
              // casting is optional but I'd suggest it for readability
assert(chuck);
for(unsigned int i=0; i<10; i++)
{
    chunk[i]=i*10;
    printf("%d",chunk[i]);
}
free(chunk);

回答by user8036942

Both code are very problematic, if you use the same pointer to send and receive from realloc, if it fails, you will lose your pointer to free it later.

两个代码都有很大的问题,如果你使用同一个指针从realloc发送和接收,如果失败,你会丢失你的指针来释放它。

you should do some thing like this :

你应该做这样的事情:

{ ... ...

{ ... ...

more = realloc(area , size);
if( more == NULL )
    free(area);
else
    area=more;

... ...

……

}

}