free 释放已分配空间

头文件

#include <stdlib.h>

语法

void free( void *ptr );

功能

函数释放指针 ptr 指向的空间,以供以后使用。指针 ptr 必须由先前对 malloc(), calloc(), realloc() 的调用返回。

示例

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char *str = NULL;

    /* allocate memory for string */
    str = calloc(10, sizeof(char));

    /* copy "Hello" into string */
    strcpy(str, "Hello");

    /* display string */
    printf("String is %s\n", str);

    /* free memory */
    free(str);

    return 0;
}

输出结果:

String is Hello
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号