realloc 的一个特点

2006年2月27日

以下文字来自 MSDN 2003 版:

realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.

 

从上面这段文字叙述来看,realloc 在无法分配指定大小的块时,将返回 NULL 指针,但却不会释放原来的块。这样一来,我们就可以放心地使用它,即使失败也不用担心数据丢失的问题。

“realloc 的一个特点” 只有一条评论

  1. Changzhe 在

    8错 🙂

留下您的评论