C语言malloc/calloc/realloc/free堆内存管理
程序员文章站
2022-03-09 15:17:49
...
1 malloc()
声明:
void* malloc (size_t size);
其中size_t代表unsigned int。
malloc:分配一块size Byte大小的内存,返回一个指向该块内存开始的指针,指针类型是void。
void * memset ( void * ptr, int value, size_t num );
示例:
#include <stdlib.h>
#include <stdio.h>
/* Micro */
#define MEMASERT(p) if(!p){sprintf(stderr,"Alloc memory failed!"); exit(-1);}
#define NUMBER_OF_STUDENT 2
/* Prototypes */
typedef struct _Student{
char * name;
char * major;
unsigned int age;
unsigned int id;
}Student;
void show_student_info(Student *pstudents, int n)
{
int i;
Student * ps;
printf("+------------+------------+----------+----------+\n");
printf("| Name | Major | Age | ID |\n");
printf("+------------+------------+----------+----------+\n");
for (i = 0, ps = pstudents; i < n; i++, ps++)
{
printf("| %-11s| %-11s| %-9s| %-9s|\n", ps->name,
ps->major, ps->age, ps->id);
}
printf("+------------+------------+----------+----------+\n");
}
int main(void)
{
Student *pstudents = NULL;
pstudents = (Student *)malloc(NUMBER_OF_STUDENT*sizeof(Student));
MEMASERT(pstudents);
memset(pstudents, 0, NUMBER_OF_STUDENT*sizeof(Student));
pstudents->name = "wwchaonj";
pstudents->major = "science";
pstudents->age = "24";
pstudents->id = "007";
printf("Student Info: \n");
show_student_info(pstudents, 2);
if (!pstudents){
free(pstudents);
pstudents = NULL;
}
system("pause");
return 0;
}
2 calloc()
声明:
void* calloc (size_t num, size_t size);
为一个大小为num的数组分配内存,每个元素的大小是size,把每个元素初始化为0。
示例:
int main(void)
{
...
Student *pstudents = NULL;
pstudents = (Student *)calloc(NUMBER_OF_STUDENT, sizeof(Student));
MEMASERT(pstudents);
...
}
3 realloc()
声明:
void* realloc (void* ptr, size_t size);
将ptr所指向的内存空间的大小改为size个字节.
如果新分配的内存比原内存大, 那么原内存的内容保持不变, 增加的空间不进行初始化.
如果新分配的内存比原内存小, 那么新内存保持原内存的内容, 增加的空间不进行初始化.
返回指向新分配空间的指针; 若内存不够,则返回NULL, 原ptr指向的内存区不变.
示例:
int main(void)
{
...
printf("Student Info: \n");
show_student_info(pstudents, 1);
printf("Realloc memory:\n");
pstudents = (Student *)realloc(pstudents, NUMBER_OF_STUDENT*sizeof(Student));
MEMASERT(pstudents);
ps = pstudents + 1;
memset(ps, 0, sizeof(Student));
ps->name = "JackMa";
ps->major = "mathmatics";
ps->age = "23";
ps->id = "008";
show_student_info(pstudents, 2);
...
}
4 free()
声明:
void free (void* ptr);
回收由malloc/calloc/realloc分配的内存空间。
5 malloc/calloc/realloc区别与联系
差异:
1) malloc/realloc不对新分配的内存进行初始化,当前值不确定;而calloc分配的内存全部初始化为0;
相同:
1) malloc/calloc/realloc均返回void*指针,使用前需要进行强制类型转换;
2) malloc/calloc/realloc分配的内存不再使用时需调用free进行内存回收。
6 参考链接:
[1] http://www.cplusplus.com/reference/cstdlib/
[2] malloc calloc realloc 作用、用法、区别、实现原理
[3] C语言动态内存管理malloc、calloc、realloc、free的用法和注意事项
推荐阅读
-
C语言中 malloc,calloc,realloc的区别
-
C语言 malloc calloc realloc 区别以及工作模式 && 预防野指针
-
C语言中 malloc,calloc,realloc的区别
-
动态内存管理及常见错误malloc()、realloc()、calloc()、free()
-
C风格的动态内存管理(malloc、calloc、realloc、free)以及总结free崩溃常见的几种情况
-
对动态内存分配函数malloc、calloc、realloc、free的理解
-
动态内存管理-malloc/realloc/calloc/free和new/delete的区别
-
C_动态分配内存:malloc,realloc,calloc,free
-
【c语言】动态内存管理(malloc、free、calloc、realloc)
-
C语言——内存管理(calloc、malloc、realloc、free)