-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDynamicMemoryAllocation.cpp
More file actions
67 lines (64 loc) · 1.03 KB
/
Copy pathDynamicMemoryAllocation.cpp
File metadata and controls
67 lines (64 loc) · 1.03 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
// DynamicMemoryAllocation
#include <iostream>
using namespace std;
int* fun(int n){
int *a = new int[n];
for(int i = 0;i<n;i++){
a[i] = i; //a[i] = *(a+i)
}
// for(int i = 0;i<n;i++){
// cout<<a[i]<<" ";
// }
return a;
}
void **Array2D(int rows, int col){
int **a = new int*[rows];
for (int i = 0; i < rows; ++i)
{
a[i] = new int[col];
}
int number=1;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < col; ++j)
{
a[i][j] = number++;
/* code */
}
/* code */
}
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < col; ++j)
{
cout<<a[i][j]<<" ";
/* code */
}
cout<<endl;
/* code */
}
}
int main(int argc, char const *argv[])
{
/* code */
int *a = new int;
*a = 10;
cout<<*a<<endl;
int n,m;
cin>>n>>m;
// int *arr = fun(n); //same array
// for(int i = 0;i<n;i++){
// cout<<arr[i]<<" ";
// }
Array2D(n,m);
//delete []arr; //delete memory from an array
//arr = NULL;
delete a; //delete memory
for (int i = 0; i < n; ++i)
{
delete []a[i];
/* code */
}
delete []a;
return 0;
}