-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdynamic_array.c
More file actions
75 lines (64 loc) · 1.31 KB
/
dynamic_array.c
File metadata and controls
75 lines (64 loc) · 1.31 KB
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
/************************************************************************/
/*
1、使用C语言方式模拟二维数组的动态开辟与释放
2、使用C++ 语言方式模拟二维数组的动态开辟与释放
3、扩展多维数组的动态开辟与释放 */
/************************************************************************/
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <stdlib.h>
#include <vld.h>
//目标是出现No memory leaks detected
#define Type int
#define ROW 3
#define COL 4
Type** _Malloc(int row, int col)
{
Type **p = (Type**)malloc(sizeof(Type*) * row); //申请数组指针空间
assert(p != NULL);
for (int i = 0; i < row; ++i)
{
p[i] = (Type*)malloc(sizeof(Type)* COL);
assert(p[i] != NULL);
}
return p;
}
void _Assign(Type **p, int row, int col)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
p[i][j] = i + j;
}
}
}
void _Print(Type **p, int row, int col)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
printf("%d ", p[i][j]);
}
printf("\n");
}
}
void _Free(Type **p, int row)
{
for (int i = 0; i < row; ++i)
{
free(p[i]);
}
free(p);
}
int main(int argc, char* argv[])
{
Type **p = _Malloc(ROW, COL);
_Assign(p, ROW, COL);
_Print(p, ROW,COL);
_Free(p, ROW);
system("pause");
return 0;
}