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 函数的使用方法的更多相关文章

  1. C语言中malloc函数的使用方法

    C语言中malloc是动态内存分配函数.函数原型:void *malloc(unsigned int num_bytes);参数:num_bytes 是无符号整型,用于表示分配的字节数.返回值:如果分 ...

  2. C语言中malloc()和calloc()c函数用法

    C语言中malloc()和calloc()c函数用法   函数malloc()和calloc()都可以用来动态分配内存空间,但两者稍有区别. malloc()函数有一个参数,即要分配的内存空间的大小: ...

  3. C语言中malloc函数返回值是否需要类型强制转换问题

    1. 在C语言中, 如果调用的函数没有函数原型, 则其返回值将默认为 int 型. 考虑调用malloc函数时忘记了 #include <stdlib.h>的情况 此时malloc函数返回 ...

  4. C语言中malloc函数的理解

    在C语言中malloc函数主要是用在堆内存的申请上,使用malloc函数时,函数会返回一个void *类型的值,这个值就是你申请的堆内存的首地址:为什么返回的地址是一个void *类型的地址呢?首先我 ...

  5. malloc,calloc,realloc三者的区别

    malloc,calloc,realloc三者都可以运用与动态分配数组. malloc:用malloc必须要自己初始化,可以用memset(arr,0,cnt*sizeof(int)) calloc: ...

  6. C:malloc/calloc/realloc/alloca内存分配函数

    原文地址:http://www.cnblogs.com/3me-linux/p/3962152.html calloc(), malloc(), realloc(), free(),alloca() ...

  7. ZH奶酪:C语言中malloc()和free()函数解析

    1.malloc()和free()的基本介绍 (1)函数原型及说明 void *malloc(long NumBytes) 该函数分配了NumBytes个字节,并返回了指向这块内存的指针.如果分配失败 ...

  8. C语言中 malloc函数用法

    一.malloc()和free()的基本概念以及基本用法: 1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针 ...

  9. malloc/calloc/realloc/alloca内存分配函数

    calloc(), malloc(), realloc(), free(),alloca() 内存区域可以分为栈.堆.静态存储区和常量存储区,局部变量,函数形参,临时变量都是在栈上获得内存的,它们获取 ...

随机推荐

  1. 解决Android后台清理APP后,程序自动重启的问题

    最近解决了一个Android APP的bug,发现APP在被后台清理后,会自动重启.现象很奇怪,有的手机(HTC)后台清理后,程序会再次重启,而有的手机(小米)则不会.猜想可能是小米手机内部做了处理, ...

  2. 慎用 supportedRuntime

    运行环境:win7, net4.5 现象: 无法连接SQL2012数据库,提示连接超时   原因:     真正的原因: 找微软去   解决的办法: 去除多余的supportedRuntime,或者修 ...

  3. 吐个槽:bose的售后真心差劲!愧对这个顶级音响产品!

    400电话只提供周一到周五(中午有1个小时非服务时间),打进去就不厌其烦地告知你服务时间,你多按几个0,对方就直接把电话给你挂了!即使耐心等待它啰嗦完,哪怕只有0个人等待或1个人等待,你也是接不进去的 ...

  4. 【译】Spring 4 + Hibernate 4 + Mysql + Maven集成例子(注解 + XML)

    前言 译文链接:http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annot ...

  5. 将Centos的yum源更换为国内的阿里云源

    阿里云是最近新出的一个镜像源.得益于阿里云的高速发展,这么大的需求,肯定会推出自己的镜像源.阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/ CentOS系统更换软 ...

  6. python ldap

    # -*- coding: UTF-8 -*- import ldap, ConfigParser, os from ldap import modlist LDAP_HOST = "myd ...

  7. SQL Server 2008 阻止保存要求重新创建表的更改问题的设置方法

    不是很理解为什么在2008中会加入阻止保存要求重新创建表的更改这个选项.症状表现为修改表结构的时候会"阻止"你.而且我遇到的情况是居然有的时候阻止你,有的时候不阻止你,摸不到头脑. ...

  8. centos7 apache httpd安装和配置django项目

    一.安装httpd服务 apache在centos7中是Apache HTTP server.如下对httpd的解释就是Apache HTTP Server.所以想安装apache其实是要安装http ...

  9. Wiki安装(PHP +Sqlite+Cache)

    前期准备 PHP http://windows.php.net/download   WinCache Extension for PHP URL:http://sourceforge.net/pro ...

  10. java的JSP技术

    java的JSP技术 [toc] 1.JSP简介 Jsp技术是用来开发java web的页面显示的,所有MVC模型里面的视图层,所以视图层的开发 jsp不是编程语言,三个英文是java server ...