#include<iostream>
#include<string>
using namespace std;
int* func(int b) { //形参也放在栈区
int * p=new int(10);
return p;
}
//2、在堆区利用new开辟数组
void test02() {//创建10整型数据的数组,在堆区
int * arr = new int[10]; //10代表数组有10个元素
for (int i = 0; i < 10; i++) {
arr[i] = i + 100; //给10个元素赋值 100~ 109
}
for (int i = 0; i < 10; i++) {
cout << arr[i] <<endl;
}
}
int main() {
int*p = func(1);
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
//堆区的数据由程序员管理开辟,程序员管理释放
//如果想释放堆区的数据,利用关键字delete
delete p;
cout << *p << endl;
system("pause");
}
还没有评论,来说两句吧...