C语言中内存管理(函数)(malloc、free、calloc、realloc)

122 篇文章 18 订阅

在这里插入图片描述
在这里插入图片描述

实例:

// c_memory_test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *p_str1, *p_str2;								// 定义两个char*指针

    /* 使用malloc()函数分配内存 */
     p_str1 = (char*)malloc(32);
     if (NULL==p_str1) {								// 检查内存分配是否成功
         printf("Alloc p_str1 memory ERROR!\n");
         return -1;
     }   
 
     /* 使用calloc()函数分配内存 */
     p_str2 = (char*)calloc(32, sizeof(char));
     if (NULL==p_str2) {								// 检查内存是否分配成功
         printf("Alloc p_str2 memory ERROR!\n");
         free(p_str1);									// 注意,这里需要释放p_str1占用的内存
         return -1;
     }   
 
     strcpy(p_str1, "This is a simple sentence.");				// p_str1写入一个字符串
     strcpy(p_str2, p_str1);								// p_str2写入与p_str1相同的字符串
 
     /* 打印p_str1的结果 */
     printf("p_str1 by malloc():\n");
     printf("p_str1 address: 0x%.8x\n", p_str1);				// p_str1的内存地址
     printf("p_str1: %s(%d chars)\n", p_str1, strlen(p_str1));		// p_str1的内容
 
     /* 打印p_str2的结果 */
     printf("p_str2 by calloc():\n");
     printf("p_str2 address: 0x%.8x\n", p_str2);				// p_str2的内存地址
     printf("p_str2: %s(%d chars)\n", p_str2, strlen(p_str2));		// p_str2的内容
 
     /* 为p_str1重新分配内存(减小) */
     p_str1 = (char*)realloc(p_str1, 16);
     if (NULL==p_str1) {								// 检查内存分配结果
         printf("Realloc p_str1 memory ERROR!\n");
         free(p_str2);									// 注意,需要释放p_str2占用的内存
         return -1;
     }   
     p_str1[15] = '\0';									// 写字符串结束符
 
     /* 为p_str2重新分配内存(增大) */
     p_str2 = (char*)realloc(p_str2, 128);
     if (NULL==p_str2) {								// 检查内存分配结果
         printf("Realloc p_str2 memory ERROR!\n");
         free(p_str1);									// 注意,需要释放p_str1占用的内存
         return -1;
     }   
     strcat(p_str2, "The second sentence in extra memory after realloced!");
 
     /* 打印p_str1的结果 */
     printf("p_str1 after realloced\n");
     printf("p_str1 address: 0x%.8x\n", p_str1);				// p_str1的内存地址
     printf("p_str1: %s(%d chars)\n", p_str1, strlen(p_str1));		// p_str1的内容
 
     /* 打印p_str2的结果 */
     printf("p_str2 after realloced:\n");
     printf("p_str2 address: 0x%.8x\n", p_str2);				// p_str2的内存地址
     printf("p_str2: %s(%d chars)\n", p_str2, strlen(p_str2));		// p_str2的内容
 
     /* 注意,最后要释放占用的内存 */
     free(p_str1);										// 释放p_str1占用的内存
     free(p_str2);										// 释放p_str2占用的内存
 
     return 0;
 }

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

嵌入式Linux系统开发

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

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

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

打赏作者

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

抵扣说明:

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

余额充值