- 論壇徽章:
- 0
|
- char *p = (char *)malloc(size);
- ....
- p = (char *)realloc(p, newsize);
復制代碼
如果realloc失敗,那么p = NULL,而此前p所指向的內存將可能產(chǎn)生丟失(因為沒有free,而現(xiàn)在我們很可能沒有記錄p以前的值)
realloc() returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and
may be different from ptr, or NULL if the request fails. If size was equal to 0, either NULL or a pointer suit-
able to be passed to free() is returned. If realloc() fails the original block is left untouched; it is not
freed or moved. |
|