-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
47 lines (33 loc) · 911 Bytes
/
Copy pathmemory.cpp
File metadata and controls
47 lines (33 loc) · 911 Bytes
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
#include <iostream>
using namespace std;
//[PowerShell] g++ .\memory.cpp -o memory | ./memory
void pointer(){
//new and delete
int *p = new int;
int *q = new int(100);
cout << "[Pointer]" << endl;
cout << "Addr:" << q << endl << "Value:" << *q << endl; //Unpredictable
*q = 200;
cout << "Addr:" << q << endl << "Value:" << *q << endl;
delete q;
cout << "Addr:" << q << endl << "Value:" << *q << endl; //Unpredictable
}
void pointer_arr(){
int size =0;
cout << "Please input array of pointer size:";
cin >> size;
int *arr = new int[size]{0};
for(int i = 0; i <size;i++){
cout << "array[" << i << "]=";
cin >> arr[i];
}
cout << "Result" << endl;
for(int i = 0; i <size;i++){
cout << "array[" << i << "]=" << *(arr+i) << endl;
}
delete [] arr;
}
int main(){
// pointer();
pointer_arr();
}