gpt4 book ai didi

c - C 中 realloc() 函数的有效性

转载 作者:行者123 更新时间:2023-11-30 18:31:45 25 4
gpt4 key购买 nike

pointer = (int*)realloc(0, sizeof(int))

一个有效的表达式?

我觉得由于realloc的第一个参数是一个指针,这里它是0(为操作系统保留的内存地址),所以这个语句不应该是一个有效的语句。有人可以评论一下该声明的有效性吗?

最佳答案

这是有效的陈述。

C 标准中的§7.22.3.5:

void *realloc(void *ptr, size_t size);

If ptr is a null pointer, the realloc function behaves like the malloc function for thespecified size. Otherwise, if ptr does not match a pointer earlier returned by a memorymanagement function, or if the space has been deallocated by a call to the free orrealloc function, the behavior is undefined. If memory for the new object cannot beallocated, the old object is not deallocated and its value is unchanged.

关于c - C 中 realloc() 函数的有效性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20879149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com