因为我已经读过如果指向的大小为 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/