C stdlib free() 函式
示例
使用完畢後釋放記憶體
// Allocate memory for a number of items
int numItems = 15;
int *myArray = calloc(numItems, sizeof(int));
// Write into the memory
for(int i = 0; i < numItems; i++) {
myArray[i] = i + 1;
}
// Display the contents of the memory
for(int i = 0; i < numItems; i++) {
printf("%d ", myArray[i]);
}
// Free the memory
free(myArray);
myArray = NULL;
自己動手試一試 »
定義和用法
free()
函式用於釋放動態記憶體。動態記憶體是由 malloc()
、calloc()
或 realloc()
函式分配的記憶體。
free()
函式定義在 <stdlib.h>
標頭檔案中。
要了解有關記憶體釋放的更多資訊,請參閱我們的 C 記憶體釋放教程。
語法
free(void * ptr);
引數值
引數 | 描述 |
---|---|
ptr | 指向動態記憶體塊的指標。 |