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() 内存区域可以分为栈.堆.静态存储区和常量存储区,局部变量,函数形参,临时变量都是在栈上获得内存的,它们获取 ...
随机推荐
- Office 365使用情况调查不完全分析报告
感谢大家参与了9月13日在Office 365技术群(O萌)中发起的一个关于Office 365使用情况的调查,在一天左右的时间内,我们一共收到了67份反馈,其中绝大部分是在3分钟内提交的. 本次调查 ...
- ArcGIS Engine开发前基础知识(1)
ArcGIS二次开发是当前gis领域的一项重要必不可少的技能.下面介绍它的基本功能 一.ArcGIS Engine功能 在使用之前首先安装和部署arcgis sdk,(在这里不在赘述相关知识)可以实现 ...
- java.lang.IllegalStateException:Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx...}: java.lang.IllegalSta ...
- 安卓---Toast工具类,有点懒
package com.liunan.myfirstapp.util; import android.content.Context; import android.widget.Toast; /** ...
- 利用sphinx为python项目生成API文档
sphinx可以根据python的注释生成可以查找的api文档,简单记录了下步骤 1:安装 pip install -U Sphinx 2:在需要生成文档的.py文件目录下执行sphinx-apido ...
- 机器学习实战笔记(Python实现)-07-分类性能度量指标
1.混淆矩阵 下图是一个二类问题的混淆矩阵,其中的输出采用了不同的类别标签 常用的衡量分类性能的指标有: 正确率(Precision),它等于 TP/(TP+FP) ,给出的是预测为正例的样本中的真正 ...
- 从零自学Hadoop(18):Hive的CLI和JDBC
阅读目录 序 Hive CLI(old CLI) Beeline CLI(new CLI) JDBC Demo下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出 ...
- Sql Server 2016新功能之 Row-Level Security
Sql Server 2016 有一个新功能叫 Row-Level Security ,大概意思是行版本的安全策略(原来我是个英语渣_(:з」∠)_) 直接上例子.这个功能相当通过对表添加一个函数作为 ...
- Ubuntu 14.04 LTS 下升级 gcc 到 gcc-4.9、gcc-5 版本
如果没记错的话,阿里云ECS上的Ubuntu也是LTS版本. 如果还在使用较旧版本的Ubuntu,或者是Ubuntu LTS,那么我们是很难体验新版gcc的.怎么办呢? 我们或许可以自己去编译用旧版本 ...
- 斐波拉契数列(Fibonacci) 的python实现方式
第一种:利用for循环 利用for循环时,不涉及到函数,但是这种方法对我种小小白来说比较好理解,一涉及到函数就比较抽象了... >>> fibs = [0,1] >>&g ...