c - 在 realloc() 之前需要 malloc() 吗?

标签 c memory-management malloc alloc

因为我已经读过如果指向的大小为 0,realloc 将充当 malloc,所以我在没有 malloc() 的情况下使用它,前提是指针是静态的、全局的,或者如果是自动的则显式设置为 NULL。

但是,我注意到很多程序员试图设置它或将其设置为 malloc(1)。有必要吗?

最佳答案

来自 Open Groups' specifications :

If ptr is a null pointer, realloc() shall be equivalent to malloc() for the specified size.

If ptr does not match a pointer returned earlier by calloc(), malloc(), or realloc() or if the space has previously been deallocated by a call to free() or realloc(), the behavior is undefined.

关于c - 在 realloc() 之前需要 malloc() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4459275/

相关文章:

c - 在 visual studio 中跟踪变量的变化

c - FILE类型的对象有哪些属性

c - 为什么 scanf() 会导致此代码中的无限循环?

r - 如何使用大文件防止 R 循环中的内存问题?

c - 为什么没有出现 "Warning C4172 returning address of local variable or temporary"错误?

c - 释放内存后我们如何访问指针?

c - 调试错误 : Heap corruption detected

c - 如何在不使用通用指针的情况下引用指向数据或数据的指针?

C 线程在访问非共享资源时给出垃圾输出

c++ - 写访问冲突 this->head 在 pop_front 中是 0xDDDDDDDD