realloc
來自 cppreference.com
在標頭檔案 <stdlib.h> 中定義 |
||
void *realloc( void *ptr, size_t new_size ); |
||
重新分配給定的記憶體區域。如果 ptr 不為 NULL,它必須是之前透過 malloc、calloc 或 realloc
分配的,並且尚未透過呼叫 free 或 realloc
釋放。否則,結果是未定義的。
重新分配透過以下任一方式完成:
a) 如果可能,擴充套件或收縮 ptr 指向的現有區域。區域內容在新舊大小中取較小值的部分保持不變。如果區域被擴充套件,則陣列新部分的內容是未定義的。
b) 分配一個大小為 new_size 位元組的新記憶體塊,複製大小等於新舊大小中較小值的記憶體區域,並釋放舊塊。
如果沒有足夠的記憶體,舊的記憶體塊不會被釋放,並返回空指標。
如果 ptr 是 NULL,則其行為與呼叫 malloc(new_size) 相同。
否則,
如果 new_size 為零,則行為是實現定義的(可能返回空指標(在這種情況下,舊記憶體塊可能被釋放,也可能不被釋放),或者可能返回不能用於訪問儲存的某個非空指標)。此類用法已棄用(透過 C DR 400)。(C17 起) |
(直至 C23) |
如果 new_size 為零,則行為未定義。 |
(自 C23 起) |
之前呼叫 free 或 |
(C11 起) |
目錄 |
[編輯] 引數
ptr | - | 指向要重新分配的記憶體區域的指標 |
new_size | - | 陣列的新大小(以位元組為單位) |
[編輯] 返回值
成功時,返回指向新分配記憶體起始的指標。為避免記憶體洩漏,返回的指標必須透過 free 或 realloc
釋放。原始指標 ptr 將失效,對其的任何訪問都是未定義行為(即使是就地重新分配)。
失敗時,返回空指標。原始指標 ptr 仍然有效,可能需要透過 free 或 realloc
釋放。
[編輯] 注意
最初(在 C89 中),新增對零大小的支援是為了適應諸如以下程式碼:
OBJ *p = calloc(0, sizeof(OBJ)); // "zero-length" placeholder /*...*/ while (1) { p = realloc(p, c * sizeof(OBJ)); // reallocations until size settles /* code that may change c or break out of loop */ }
[編輯] 示例
執行此程式碼
#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void print_storage_info(const int* next, const int* prev, int ints) { if (next) printf("%s location: %p. Size: %d ints (%ld bytes).\n", (next != prev ? "New" : "Old"), (void*)next, ints, ints * sizeof(int)); else printf("Allocation failed.\n"); } int main(void) { const int pattern[] = {1, 2, 3, 4, 5, 6, 7, 8}; const int pattern_size = sizeof pattern / sizeof(int); int *next = NULL, *prev = NULL; if ((next = (int*)malloc(pattern_size * sizeof *next))) // allocates an array { memcpy(next, pattern, sizeof pattern); // fills the array print_storage_info(next, prev, pattern_size); } else return EXIT_FAILURE; // Reallocate in cycle using the following values as a new storage size. const int realloc_size[] = {10, 12, 512, 32768, 65536, 32768}; for (int i = 0; i != sizeof realloc_size / sizeof(int); ++i) { if ((next = (int*)realloc(prev = next, realloc_size[i] * sizeof(int)))) { print_storage_info(next, prev, realloc_size[i]); assert(!memcmp(next, pattern, sizeof pattern)); // is pattern held } else // if realloc failed, the original pointer needs to be freed { free(prev); return EXIT_FAILURE; } } free(next); // finally, frees the storage return EXIT_SUCCESS; }
可能的輸出
New location: 0x144c010. Size: 8 ints (32 bytes). Old location: 0x144c010. Size: 10 ints (40 bytes). New location: 0x144c450. Size: 12 ints (48 bytes). Old location: 0x144c450. Size: 512 ints (2048 bytes). Old location: 0x144c450. Size: 32768 ints (131072 bytes). New location: 0x7f490c5bd010. Size: 65536 ints (262144 bytes). Old location: 0x7f490c5bd010. Size: 32768 ints (131072 bytes).
[編輯] 參考
- C23 標準 (ISO/IEC 9899:2024)
- 7.22.3.5 realloc 函式 (p: 待定)
- C17 標準 (ISO/IEC 9899:2018)
- 7.22.3.5 realloc 函式 (p: 254)
- C11 標準 (ISO/IEC 9899:2011)
- 7.22.3.5 realloc 函式 (p: 349)
- C99 標準 (ISO/IEC 9899:1999)
- 7.20.3.4 realloc 函式 (p: 314)
- C89/C90 標準 (ISO/IEC 9899:1990)
- 4.10.3.4 realloc 函式
[編輯] 另請參閱
C++ 文件 關於 realloc
|