-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdynamic_array.cpp
75 lines (65 loc) · 1.33 KB
/
dynamic_array.cpp
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 //char int
#define ROW 3
#define COL 4
Type** _New(int row, int col)
{
Type **p = new Type *[row];//申请数组指针空间
assert(p != NULL);
for (int i = 0; i < row; ++i)
{
p[i] = new 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 _Delete(Type **p, int row)
{
for (int i = 0; i < row; ++i)
{
delete[]p[i];//c++ 释放数组空间
}
delete[]p;//c++释放数组
}
int main(int argc, char* argv[])
{
Type **p = _New(ROW, COL);
_Assign(p, ROW, COL);
_Print(p, ROW, COL);
_Delete(p, ROW);
printf("c++\n");
system("pause");
return 0;
}