C 语言中 malloc、calloc、realloc 和free 函数的使用方法
C标准函数库中,常见的堆上内存管理函数有malloc(), calloc(), recalloc(), free()。
之所以使用堆,是因为栈只能用来保存临时变量、局部变量和函数参数。在函数返回时,自动释放所占用的存储空间。而堆上的内存空间不会自动释放,直到调用free()函数,才会释放堆上的存储空间。
一、具体使用方法
1、malloc()
头文件:stdlib.h
声明:void * malloc(int n);
含义:在堆上,分配n个字节,并返回void指针类型。
返回值:分配内存成功,返回分配的堆上存储空间的首地址;否则,返回NULL
2、calloc()
头文件:stdlib.h
声明:void *calloc(int n, int size);
含义:在堆上,分配n*size个字节,并初始化为0,返回void* 类型
返回值:同malloc() 函数
3、recalloc()
头文件:stdlib.h
声明:void * realloc(void * p,int n);
含义:重新分配堆上的void指针p所指的空间为n个字节,同时会复制原有内容到新分配的堆上存储空间。注意,若原来的void指针p在堆上的空间不大于n个字节,则保持不变。
返回值:同malloc() 函数
4、free()
头文件:stdlib.h
声明:void free (void * p);
含义:释放void指针p所指的堆上的空间。
返回值:无
5、memset()
头文件:string.h
声明:void * memset (void * p, int c, int n) ;
含义:对于void指针p为首地址的n个字节,将其中的每个字节设置为c。
返回值:返回指向存储区域 p 的void类型指针。
二、示例代码
/*
* Author: klchang
* Description:
Test the memory management functions in heap.
* Created date: 2016.7.29
*/ #include <stdio.h> // scanf, printf
#include <stdlib.h> // malloc, calloc, realloc, free
#include <string.h> // memset #define SIZE 10 // Input Module
int* inputModule(int* ptrCount)
{
int* arr, d, i = ;
int length = SIZE; // Apply malloc()
arr = (int*)malloc(SIZE * sizeof(int));
memset(arr, , length * sizeof(int)); // Input module
printf("Input numbers until you input zero: \n");
while () {
scanf("%d", &d);
// count
*ptrCount += ;
if ( == d) {
arr[i] = ;
break;
} else {
arr[i++] = d;
}
if (i >= length) {
// Apply realloc()
realloc(arr, *length*sizeof(int));
memset(arr+length, , length * sizeof(int));
length *= ;
}
} return arr;
} // Output module
void outputModule(int* arr, int* ptrCount)
{
int i; printf("\nOutput all elements that have been input: \n");
for(i = ; i < *ptrCount; i++) {
if (i && i% == )
printf("\n");
printf("\t%d", *(arr+i));
} // Release heap memory space
free(ptrCount);
free(arr);
} int main()
{
int i = ;
int* ptrCount;
int* arr; // Apply calloc()
ptrCount = (int *)calloc(, sizeof(int));
// Input Module
arr = inputModule(ptrCount);
// Before free() function, output the count of input numbers
printf("\n\nBefore using free() function, Count: %d", *ptrCount);
// Output Module
outputModule(arr, ptrCount);
// After free() function, output the count of input numbers
printf("\n\nAfter using free() function, Count: %d", *ptrCount); return ;
}
结果图片

参考资料:
1、C中堆管理——浅谈malloc,calloc,realloc函数之间的区别
http://www.cppblog.com/sandywin/archive/2011/09/14/155746.html
C 语言中 malloc、calloc、realloc 和free 函数的使用方法的更多相关文章
- C语言中malloc函数的使用方法
C语言中malloc是动态内存分配函数.函数原型:void *malloc(unsigned int num_bytes);参数:num_bytes 是无符号整型,用于表示分配的字节数.返回值:如果分 ...
- C语言中malloc()和calloc()c函数用法
C语言中malloc()和calloc()c函数用法 函数malloc()和calloc()都可以用来动态分配内存空间,但两者稍有区别. malloc()函数有一个参数,即要分配的内存空间的大小: ...
- C语言中malloc函数返回值是否需要类型强制转换问题
1. 在C语言中, 如果调用的函数没有函数原型, 则其返回值将默认为 int 型. 考虑调用malloc函数时忘记了 #include <stdlib.h>的情况 此时malloc函数返回 ...
- C语言中malloc函数的理解
在C语言中malloc函数主要是用在堆内存的申请上,使用malloc函数时,函数会返回一个void *类型的值,这个值就是你申请的堆内存的首地址:为什么返回的地址是一个void *类型的地址呢?首先我 ...
- malloc,calloc,realloc三者的区别
malloc,calloc,realloc三者都可以运用与动态分配数组. malloc:用malloc必须要自己初始化,可以用memset(arr,0,cnt*sizeof(int)) calloc: ...
- C:malloc/calloc/realloc/alloca内存分配函数
原文地址:http://www.cnblogs.com/3me-linux/p/3962152.html calloc(), malloc(), realloc(), free(),alloca() ...
- ZH奶酪:C语言中malloc()和free()函数解析
1.malloc()和free()的基本介绍 (1)函数原型及说明 void *malloc(long NumBytes) 该函数分配了NumBytes个字节,并返回了指向这块内存的指针.如果分配失败 ...
- C语言中 malloc函数用法
一.malloc()和free()的基本概念以及基本用法: 1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针 ...
- malloc/calloc/realloc/alloca内存分配函数
calloc(), malloc(), realloc(), free(),alloca() 内存区域可以分为栈.堆.静态存储区和常量存储区,局部变量,函数形参,临时变量都是在栈上获得内存的,它们获取 ...
随机推荐
- Storm中遇到的日志多次重写问题(一)
业务描述: 统计从kafka spout中读取的数据条数,以及写入redis的数据的条数,写入hdfs的数据条数,写入kafaka的数据条数.并且每过5秒将数据按照json文件的形式写入日志.其中保存 ...
- ABP督导项目(1)
创建实体 项目名TQMASP 在领域层创建entities文件夹存放实体类如图 创建Dbcontext public virtual IDbSet<Supervisor> Supervis ...
- redis 缓存技术与memcache的区别
1 什么是redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset( ...
- Juniper SSG5 PPTP VPN 619错误解决
公司分部的客户端需要使用PPTP VPN连接总部,将网关更换为Juniper SSG5后,客户端出现了每几个小时自动断开的现象,错误619. 解决:Security —— ALG —— 开启PPTP协 ...
- Ubuntu管理开机启动服务项 -- 图形界面的Boot-up Manager
有时学习时安装的服务太多,比如mysql.mongodb.redis.apache.nginx等等,它们都是默认开机启动的,如果不想让它们开机启动,用到时再自己手工启动怎么办呢? 使用sysv-rc- ...
- 卷积神经网络(CNN)学习算法之----基于LeNet网络的中文验证码识别
由于公司需要进行了中文验证码的图片识别开发,最近一段时间刚忙完上线,好不容易闲下来就继上篇<基于Windows10 x64+visual Studio2013+Python2.7.12环境下的C ...
- S5PV210_uart stdio移植
1.stdio : standard input output 标准输入输出 2.printf函数调用到的2个关键函数: vsprintf : 格式化打印信息,最终得到纯字符串的打印信息等待输出 pu ...
- C++类继承关系视图的自动生成
原创文章,转载请注明出处. 工欲善其事,必先利其器.阅读大型C++工程项目,如果有一些自动化的分析工具支持,学习的效率将大大提升.在前文中介绍了Source Insight在Linux下的安装方法,本 ...
- redis3.0常用命令
1.服务器启动 1)快捷启动 $redis-server 2)指定配置文件启动 $redis-server redis.conf 2.客服端启动 1)快捷无密码启动 $redis-cli 2)有密码和 ...
- PIC10F200/202/204/206/220/222/320/322芯片解密程序复制多少钱?
PIC10F200/202/204/206/220/222/320/322芯片解密程序复制多少钱? PIC10F单片机芯片解密型号: PIC10F200解密 | PIC10F202解密 | PIC10 ...