1 //new在堆区开辟数据,手动开辟,手动释放 2 #include <iostream> 3 using namespace std; 4 //1.new的基本语法 5 int* func() 6 { 7 //new返回的是指针 8 int* p = new int(10); 9 return p; 10 } 11 void test01() 12 { 13 int* p = func(); 14 cout << *p << endl; 15 delete p; 16 } 17 //2.在堆区创建整型数组 18 void test02() 19 { 20 //创建10个整型数据数组 21 int *arr = new int[10]; 22 for (int i = 0; i < 10; i++) 23 { 24 arr[i] = i + 100; 25 } 26 for (int i = 0; i < 10; i++) 27 { 28 cout << arr[i]<< endl; 29 } 30 //delete数组 31 delete[] arr; 32 } 33 int main() 34 { 35 test01(); 36 test02(); 37 return 0; 38 }
标签:10,2023.5,int,手动,func,new From: https://www.cnblogs.com/muzhaodi/p/17375645.html