-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew.cpp
More file actions
60 lines (49 loc) · 1022 Bytes
/
Copy pathnew.cpp
File metadata and controls
60 lines (49 loc) · 1022 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
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// Created by zing on 4/4/2020.
//
#include <iostream>
using namespace std;
void new1() {
double *d = nullptr; // 初始化为 null 的指针
d = new double; // 为变量请求内存
*d = 10;
cout << *d << endl;//10
double d2 = 11;
d = &d2;
cout << *d << endl;//11
*d = 12;
cout << *d << endl; //12
cout << d2 << endl; // 12
}
int *func() {
int *a = new int(10);
return a;
}
int new2() {
int *p = func();
cout << *p << endl;
cout << *p << endl;
//利用delete释放堆区数据
delete p;
//cout << *p << endl; //报错,释放的空间不可访问
return 0;
}
//堆区开辟数组
int new_array() {
int *arr = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = i + 100;
}
for (int i = 0; i < 10; i++) {
cout << arr[i] << endl;
}
//释放数组 delete 后加 []
delete[] arr;
return 0;
}
int main(int argc, char *argv[]) {
new1();
new2();
new_array();
return 0;
}