1. 动态内存分配的意义
(1)C 语言中的一切操作都是基于内存的。
(2)变量和数组都是内存的别名。
①内存分配由编译器在编译期间决定
②定义数组的时候必须指定数组长度
③数组长度是在编译期就必须确定的
(3)但是程序运行的过程中,可能需要使用一些额外的内存空间
2. malloc 和 free 函数
(1)malloc 和 free 用于执行动态内存分配的释放
(2)malloc 所分配的是一块连续的内存
(3)malloc 以字节为单位,并且返回值不带任何的类型信息:void* malloc(size_t size);
(4)free 用于将动态内存归还系统:void free(void* pointer);
(5)_msize(void* pointer)可以获取 malloc 出来的内存空间大小
3. 使用 malloc 和 free 需要注意的地方
(1)malloc 和 free 是库函数,而不是系统调用
(2)malloc 实际分配的内存可能有会比请求的多,但不能依赖于不同平台下的 malloc 行为。
(3)当请求的动态内存无法满足时,malloc 返回 NULL
(4)当 free 的参数为 NULL 时,函数直接返回
malloc(0)返回什么?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <stdio.h>
#include <malloc.h>
int main()
{
int i=10;
int * p= NULL;
for (i=0;i<100;i++)
{
//注意,malloc(0)会返回一个有效的内存地址,大小为1
//但我们不能依赖编译器的这种行为来使用这个字节的空间!
p = ( int *) malloc (i);
printf ( "%d " ,_msize(p)); //返回malloc出来的内存空间大小
free (p);
}
return 0;
}
|
内存泄漏检测模块
mleak.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#ifndef _MLEAK_H_
#define _MLEAK_H_
#include <stdio.h>
#include <malloc.h>
#define MALLOC(n) mallocEx(n, __FILE__, __LINE__)
#define FREE(p) freeEx(p)
void * mallocEx( size_t n, const char * file, const line);
void freeEx( void * p);
void PRINT_LEAK_INFO();
#endif
|
mleak.c
复制代码
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
#include "mleak.h"
#define SIZE 256
//动态内存申请参数结构体
typedef struct
{
void * pointer; //申请到的内存地址
int size; //内存块大小
const char * file; //文件名
int line; //文件行号
}MItem;
static MItem g_record[SIZE]; //记录每个动态内存申请的操作
void * mallocEx( size_t n, const char * file, const line)
{
int i = 0;
void * ret = malloc (n); //动态内存申请
if (ret != NULL)
{
//申请成功,则记录下来
//遍历全局数组,记录此次操作
for (i = 0; i< SIZE; i++)
{
//查找位置
if (g_record[i].pointer == NULL)
{
g_record[i].pointer = ret;
g_record[i].size = n;
g_record[i].file = file;
g_record[i].line = line;
break ;
}
}
}
return ret;
}
void freeEx( void * p)
{
if (p != NULL)
{
int i = 0;
//遍历全局数组,释放内存空间,并清除操作记录
for (i = 0; i< SIZE; i++)
{
if (g_record[i].pointer == p)
{
g_record[i].pointer = NULL;
g_record[i].size = 0;
g_record[i].file = NULL;
g_record[i].line = 0;
free (p);
break ;
}
}
}
}
void PRINT_LEAK_INFO()
{
int i = 0;
printf ( "Potenital Memory Leak Info:\n" );
//遍历全局数组,打印未释放的空间的申请记录
for (i = 0; i< SIZE; i++)
{
//查找位置
if (g_record[i].pointer != NULL)
{
printf ( "Address:%p, size:%d, Location:%s:%d\n" ,
g_record[i].pointer,
g_record[i].size,
g_record[i].file,
g_record[i].line);
}
}
}
|
testc.
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
|
#include <stdio.h>
#include "mleak.h"
void f()
{
//没释放,会造成内存泄漏!
MALLOC(100);
}
int main()
{
int * p = ( int *)MALLOC(3 * sizeof ( int ));
f();
p[0] = 1;
p[1] = 2;
p[2] = 3;
FREE(p);
PRINT_LEAK_INFO();
return 0;
}
/*
输出结果:
E:\Study>gcc test.c mleak.c
E:\Study>a.exe
Potenital Memory Leak Info:
Address:00602ED8, size:100, Location:38-1.c:7
*/
|
4. calloc 和 realloc 函数
(1)malloc 的同胞兄弟:
void* calloc(size_t num, size_t size);
void* realloc(void* pointer,size_t new_size);
(2)calloc 参数表示要返回 num 个某种类型(如 sizeof(int))大小的内存空间。calloc 能以类型大小为单位申请内存并初始化为 0.
(3)realloc 用于修改一个原先己经分配的内存块大小。当第一个参数 pointer 为 NUL 时,等价于 malloc。
calloc 和 realloc 的使用
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
|
#include <stdio.h>
#include <malloc.h>
#define SIZE 5
int main()
{
int i = 0 ;
int * pI = ( int *)malloc(SIZE * sizeof( int )); //malloc内存没有初始化
short * pS = ( short *)calloc(SIZE, sizeof( short )); //内存初始化为0
for (i = 0 ; i < SIZE;i++)
{
printf( "pI[%d] = %d, pS[%d] = %d\n" , i, pI[i], i, pS[i]);
}
printf( "Before: pI = %p\n" , pI); //重置内存大小之前的pI指针
pI = ( int *)realloc(pI, 2 * SIZE * sizeof( int )); //内存未初始化的
printf( "After: pI = %p\n" , pI);
for (i = 0 ; i < 10 ;i++)
{
printf( "pI[%d] = %d\n" , i, pI[i]);
}
free(pI);
free(pS);
return 0 ;
}
|
通过此文希望大家对C语言的动态内存分配了解掌握,谢谢大家对本站的支持!