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() 内存区域可以分为栈.堆.静态存储区和常量存储区,局部变量,函数形参,临时变量都是在栈上获得内存的,它们获取 ...
随机推荐
- Android之RecyclerView的原生Bug-Inconsistency detected. Invalid view holder adapter positionViewHolder{a1bbfa3 position=2 id=-1, oldPos=-1, pLpos:-1 no parent}
今天在运行自己编写的App时,突然发现App在运行时闪退,然后就查看了Android Studio的Log,发现了这个错误,上网查了一下,才知道是RecyclerView的原生Bug,在数据更新时会出 ...
- HTML最新标准HTML5小结
写在前面 HTML5出来已经很久了,然而由于本人不是专业搞前端的,只知道有这个东西,具体概念有点模糊(其实就是一系列标准规范啦):因此去年(2015.11.09),专门对HTML5做了个简单的小结,今 ...
- 0035 Java学习笔记-注解
什么是注解 注解可以看作类的第6大要素(成员变量.构造器.方法.代码块.内部类) 注解有点像修饰符,可以修饰一些程序要素:类.接口.变量.方法.局部变量等等 注解要和对应的配套工具(APT:Annot ...
- [转]OAuth 2.0 - Authorization Code授权方式详解
本文转自:http://www.cnblogs.com/highend/archive/2012/07/06/oautn2_authorization_code.html I:OAuth 2.0 开发 ...
- swt shell设置窗口位于屏幕中间
/** * 设置窗口位于屏幕中间 * @param shell 要调整位置的窗口对象 */ public static void center(Shell shell) ...
- [LeetCode] Nth Digit 第N位
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- MVC使用ajax异步刷新时怎样输出从后台中传过来的JSON数据
前言 这几天在学习MVC使用AJAX异步刷,因为是新手.所以在js中传参数到后台以及后台返回数据到前台怎么接受,怎么前台遍历出JSON数据都开始不知道,相信新手在使用时跟我一样会遇到,这里我就和大家分 ...
- asp.net获取服务器绝对路径和相对路径
绝对路径 AppDomain.CurrentDomain.SetupInformation.ApplicationBase 相对路径 Server.MapPath("~/")表示当 ...
- 详解mysql如何配置远程链接,解决各种连接问题
在服务器上面我们经常需要去使用mysql,有些童鞋刚刚配置好了服务器,想在本地的一些图形化软件去连接mysql得到更直观的表格显示,此时很可能不允许连接,为了探究为什么连接失败,在这里我会对mysql ...