【发布时间】:2020-08-16 04:23:58
【问题描述】:
所以,在编写程序时,我意识到当在 main 之外的函数中使用 realloc 时,如果在 main 中声明了原始内存块,它似乎不会将更改保留在函数之外。 例如
void main()
{
int *ptr;
//allocates memory
ptr = calloc(4, sizeof(int));
exampleFunction(&ptr);
} //end main
//this function reallocates the memory block of ptr
void exampleFunction(int *ptr)
{
ptr = realloc(ptr, (sizeof(int) * 10));
} // end exampleFunction
我需要做一些不同的事情还是应该可以正常工作? 此外,这只是示例代码,不能运行
额外信息 我在 Windows 10 上使用 MinGW
【问题讨论】:
-
尽量让您的程序保持 0(零)警告。现在你收到了警告,不想阅读它们,而是更愿意寻求帮助。
-
所以,正如我在原始帖子中所说的那样,这是示例代码,而不是我的实际代码,我想确保这是我可以做的可行的事情
标签: c pass-by-reference dynamic-memory-allocation realloc