malloc函数应用
来源:互联网 发布:网络路演 编辑:程序博客网 时间:2024/06/09 16:45
从本质上来说,malloc(Linux上具体实现可以参考man malloc,glibc通过brk()&mmap()实现)是libc里面实现的一个函数,如果在source code中没有直接或者间接include过stdlib.h,那么gcc就会报出error:‘malloc’ was not declared in this scope。如果生成了目标文件(假定动态链接malloc),如果运行平台上没有libc(Linux平台,手动指定LD_LIBRARY_PATH到一个空目录即可),或者libc中没有malloc函数,那么会在运行时(Run-time)出错。new则不然,是c++的关键字,它本身不是函数。new不依赖于头文件,c++编译器就可以把new编译成目标代码(g++4.6.3会向目标中插入_Znwm这个函数,另外,编译器还会根据参数的类型,插入相应的构造函数)。
在使用上,malloc 和 new 至少有两个不同: new 返回指定类型的指针,并且可以自动计算所需要大小。比如:
1
2
3
int
*p;
p=
new
int
;
//返回类型为int*类型(整数型指针),分配大小为sizeof(int);
或:
1
2
3
int
*parr;
parr=
new
int
[100];
//返回类型为int*类型(整数型指针),分配大小为sizeof(int)*100;
而 malloc 则必须要由我们计算字节数,并且在返回后强行转换为实际类型的指针。
1
2
3
4
5
6
7
int
*p;
p=(
int
*)
malloc
(
sizeof
(
int
)*128);
//分配128个(可根据实际需要替换该数值)整型存储单元,
//并将这128个连续的整型存储单元的首地址存储到指针变量p中
double
*pd=(
double
*)
malloc
(
sizeof
(
double
)*12);
//分配12个double型存储单元,
//并将首地址存储到指针变量pd中
第一、malloc 函数返回的是 void * 类型。对于C++,如果你写成:p = malloc (sizeof(int)); 则程序无法通过编译,报错:“不能将 void* 赋值给 int * 类型变量”。所以必须通过 (int *) 来将强制转换。而对于C,没有这个要求,但为了使C程序更方便的移植到C++中来,建议养成强制转换的习惯。
第二、函数的实参为 sizeof(int) ,用于指明一个整型数据需要的大小。
在Linux中可以有这样:malloc(0),这是因为Linux中malloc有一个下限值16Bytes,注意malloc(-1)是禁止的;
但是在某些系统中是不允许malloc(0)的。在规范的程序中我们有必要按照这样的格式去使用malloc及free:
1
2
3
4
5
6
7
8
9
10
type*p;
if
(NULL==(p=(type*)
malloc
(
sizeof
(type))))
/*请使用if来判断,这是有必要的*/
{
perror
(
"error..."
);
exit
(1);
}
...
/*其它代码*/
free
(p);
p=NULL;
/*请加上这句*/
malloc 也可以达到 new [] 的效果,申请出一段连续的内存,方法无非是指定你所需要内存大小。
比如想分配100个int类型的空间:
1
2
int
*p=(
int
*)
malloc
(
sizeof
(
int
)*100);
//分配可以放得下100个整数的内存空间。
另外有一点不能直接看出的区别是,malloc 只管分配内存,并不能对所得的内存进行初始化,所以得到的一片新内存中,其值将是随机的。
除了分配及最后释放的方法不一样以外,通过malloc或new得到指针,在其它操作上保持一致。
对其做一个特例补充
1
2
3
4
5
char
*ptr;
if
((ptr=(
char
*)
malloc
(0))==NULL)
puts
(
"Gotanullpointer"
);
else
puts
(
"Gotavalidpointer"
);
此时得到的是Got a valid pointer。把0赋给malloc能得到一个合法的指针。
原理是:
malloc函数的实质体现在,它有一个将可用的内存块连接为一个长长的列表的所谓空闲链表。调用malloc函数时,它沿连接表寻找一个大到足以满足用户请求所需要的内存块。然后,将该内存块一分为二(一块的大小与用户请求的大小相等,另一块的大小就是剩下的字节)。接下来,将分配给用户的那块内存传给用户,并将剩下的那块(如果有的话)返回到连接表上。调用free函数时,它将用户释放的内存块连接到空闲链上。到最后,空闲链会被切成很多的小内存片段,如果这时用户申请一个大的内存片段,那么空闲链上可能没有可以满足用户要求的片段了。于是,malloc函数请求延时,并开始在空闲链上翻箱倒柜地检查各内存片段,对它们进行整理,将相邻的小空闲块合并成较大的内存块。如果无法获得符合要求的内存块,malloc函数会返回NULL指针,因此在调用malloc动态申请内存块时,一定要进行返回值的判断。
正常程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
typedef
struct
data_type{
intage;
charname[20];
}data;
data*bob=NULL;
bob=(data*)
malloc
(
sizeof
(data));
if
(bob!=NULL)
{
bob->age=22;
strcpy
(bob->name,
"Robert"
);
printf
(
"%sis%dyearsold\n"
,bob->name,bob->age);
}
else
{
printf
(
"mallocerror!\n"
);
exit
(-1);
}
free
(bob);
bob=NULL;
内存泄漏实例
1
2
3
4
5
6
7
8
9
10
11
12
13
例1:
#include<stdio.h>
#include<malloc.h>
#defineMAX100000000
intmain(
void
)
{
int
*a[MAX]={NULL};
inti;
for
(i=0;i<MAX;i++){
a[i]=(
int
*)
malloc
(MAX);
}
return0;
}
例1:
对
malloc
申请之后没有检测返回值;
例2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include"stdio.h"
#include"malloc.h"//malloc()函数被包含在malloc.h里面
int
main(
void
)
{
char
*a=NULL;
//声明一个指向a的char*类型的指针
a=(
char
*)
malloc
(100*
sizeof
(
char
));
//使用malloc分配内存的首地址,然后赋值给a
if
(!a)
//如果malloc失败,可以得到一些log
{
perror
(
"malloc"
);
return
-1;
}
sprintf
(a,
"%s"
,
"HelloWorld\n"
);
//"HelloWorld\n"写入a指向的地址
printf
(
"%s\n"
,a);
//输出用户输入的数据
free
(a);
//释放掉使用的内存地址
return0;
//例2有无内存泄露?
}
例2:
检测
malloc
返回值条件有误
0 0
- malloc函数应用
- DSP开发中malloc函数应用问题
- 系统函数的应用——malloc realloc calloc函数
- malloc函数
- malloc函数
- malloc函数
- malloc函数
- malloc ()函数
- malloc函数
- Malloc函数
- malloc函数
- malloc函数
- Malloc函数
- malloc函数
- malloc函数
- malloc函数
- malloc函数
- malloc函数
- cxf的spring启动测试配置
- 插入排序Insertion Sort
- 2-SAT问题的解法(uva1146)
- git简单使用入门总结
- MVC 模式中正向传值、反向传值
- malloc函数应用
- putty免密码实现自动登录远程主机
- 正负数排序
- [C++11]新特性试用
- 安卓获取星期
- 跟我一起学Makefile
- hdu 5148 City (树形dp)
- Remove Linked List Elements - LeeCode 203
- 数据挖掘的方法有哪些?