如何在C中的函数中使用realloc [duplicate]

时间:2022-09-06 08:08:13

This question already has an answer here:

这个问题已经有了答案:

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

基于我在这里学到的:通过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 realloc in 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 realloc to a variable and check if it is NULL first. This is just a simplified example.

我理解我应该将realloc的结果分配给一个变量,并首先检查它是否为NULL。这只是一个简化的例子。

3 个解决方案

#1


27  

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

您想要修改int*(您的数组)的值,因此需要将指向它的指针传递到增加函数中:

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

#2


5  

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. 使用realloc;
  3. Check if it's NULL.In this case use perror and exit the program;
  4. 检查如果是NULL。在本例中使用perror并退出程序;
  5. If it's not NULL use the memory allocated;
  6. 如果它不是NULL,则使用分配的内存;
  7. Free the memory when you don't need it anymore.
  8. 当你不再需要内存时,释放内存。

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

#3


2  

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;

... ...

……

}

}

#1


27  

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

您想要修改int*(您的数组)的值,因此需要将指向它的指针传递到增加函数中:

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

#2


5  

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. 使用realloc;
  3. Check if it's NULL.In this case use perror and exit the program;
  4. 检查如果是NULL。在本例中使用perror并退出程序;
  5. If it's not NULL use the memory allocated;
  6. 如果它不是NULL,则使用分配的内存;
  7. Free the memory when you don't need it anymore.
  8. 当你不再需要内存时,释放内存。

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

#3


2  

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;

... ...

……

}

}