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 | 指向动态内存块的指针。 |