C语言-动态内存管理

本文介绍了C语言中动态内存管理的四个关键函数:malloc()用于分配指定大小的内存空间,calloc()用于分配并初始化连续内存,realloc()用于调整已分配内存的大小,free()用于释放不再需要的内存。通过示例代码展示了如何使用这些函数进行内存操作,并强调了动态内存管理在避免内存溢出和优化资源利用方面的重要性。
摘要由CSDN通过智能技术生成

前言:

简单记录一下,内存管理函数

  • 为什么使用动态内存呢?
  • 简单理解就是可以最大限度调用内存
  • 用多少生成多少,不用时就释放
  • 而静止内存不能释放
  • 动态可避免运行大程序导致内存溢出

C 语言为内存的分配和管理提供了几个函数:

头文件:<stdlib.h>

注意:void * 类型表示未确定类型的指针 


1.malloc() 用法

 分配一块大小为 num 的内存空间

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

int main() {
    char name[12];
    char *test;

    strcpy(name, "KiKiNiNi");

    // 动态分配内存
    test = (char *) malloc(26 * sizeof(char));

    // (void *) malloc(int num) -> num = 26 * sizeof(char)
    // void * 表示 未确定类型的指针
    // 分配了一块内存空间 大小为 num 存放值是未知的

    if (test == NULL) {
        fprintf(stderr, "Error - unable to allocate required memory\n");
    } else {
        strcpy(test, "Maybe just like that!");
    }

    printf("Name = %s\n", name);
    printf("Test: %s\n", test);

    return 0;
}

// 运行结果
// Name = KiKiNiNi
// Test: Maybe just like that!

2.calloc() 用法

 分配 num 个长度为 size 的连续空间

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

int main() {
    char name[12];
    char *test;

    strcpy(name, "KiKiNiNi");

    // 动态分配内存
    test = (void *) calloc(26, sizeof(char));

    // (void *) calloc(int num, int size) -> num = 26 / size = sizeof(char)
    // void * 表示 未确定类型的指针
    // 分配了 num 个 大小为 size 的连续空间 存放值初始化为 0

    if (test == NULL) {
        fprintf(stderr, "Error - unable to allocate required memory\n");
    } else {
        strcpy(test, "Maybe just like that!");
    }

    printf("Name = %s\n", name);
    printf("Test: %s\n", test);

    return 0;
}

// 运行结果
// Name = KiKiNiNi
// Test: Maybe just like that!

3.realloc() 与 free() 用法

重新调整内存的大小和释放内存

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

int main() {
    char name[12];
    char *test;

    strcpy(name, "KiKiNiNi");

    // 动态分配内存
    test = (char *) malloc(26 * sizeof(char));

    // (void *) malloc(int num) -> num = 26 * sizeof(char)
    // void * 表示 未确定类型的指针
    // 分配了一块内存空间 大小为 num 存放值是未知的

    if (test == NULL) {
        fprintf(stderr, "Error - unable to allocate required memory\n");
    } else {
        strcpy(test, "Maybe just like that!");
    }

    /* 假设您想要存储更大的描述信息 */
    test = (char *) realloc(test, 100 * sizeof(char));
    if (test == NULL) {
        fprintf(stderr, "Error - unable to allocate required memory\n");
    } else {
        strcat(test, " It's a habit to love her.");
    }

    printf("Name = %s\n", name);
    printf("Test: %s\n", test);

    // 释放 test 内存空间
    free(test);

    return 0;
}

// 运行结果
// Name = KiKiNiNi
// Test: Maybe just like that! It's a habit to love her.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃饭用勺子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值