C++的new和delete
一,new和delete:
(1)new是操作符,不是函數,malloc是函數。
(2)基本數據類型(int?,float,double,char),free, malloc, new,delete?效果一致。
(3)delete ,free對于基本數據類型,釋放,釋放兩次都會出錯。
(4)free之后,指針的值不會改變,delete之后,指針的值會改變(我的電腦上每次delete之后,指針的值變成了0000 8123),而free之后的指針的值沒有改變,打印出來的是垃圾值。free之后,為了避免迷途指針,最好將指針設置為空指針(nullptr)。
(5)如果是基本數據類型?new?是不會調用構造函數,delete?時不會調用析構函數。
如果是結構類型,new會調用構造函數,delete會調用析構函數。
(6)基本數據類型?數組,delete,delete[]一致;基本數據類型,可以互相混合使用。
(7)復合數據類型(delete??,delete[]不能混合使用)
例如:mydata *p(new mydata);?? delete p;
mydata *p(new mydata[10]);??? delete[] p;
(8)單個堆上的對象,不可以用delete [],反復delete,下面delet代碼是錯誤,只用能delete p,不能用delete [] p.
mydata *p(new mydata);
delete [?]??p;//單個堆上的對象,不可以用?delete [],用delete[]程序會報錯
#include<iostream> using namespace std;void main2() {int *p1 = new int(4);//指向一個變量int *p2 = new int[10]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };//指向數組delete p1;//delete p1 int *p1 = new int(4);delete[] p2;//delete[] p2 int *p2 = new int[10]{ 1,2,3,4,5,6,7,8,9,0 }; cin.get(); }//數組new和初始化 void main3() {int *p = new int[10]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };cout << p << endl;cout << "--------------" << endl;delete[] p;cin.get(); }
//基本數據類型new及初始化 void main4() {int *p1 = new int(4);cout << p1 << endl;cout << "--------------" << endl;delete p1;//delete[] p1;cin.get(); }
class mydata { public:mydata() //構造函數 {cout << "create" << endl;}~mydata()//析構函數 {cout << "delete" << endl;} };
void main() {mydata *p(new mydata);delete p;//mydata *p(new mydata);//delete []p;//單個堆上的對象,不可以 delete [],反復deletemydata *p(new mydata[10]);delete []p;//異常cin.get(); } 二,定位new運算符: 定位new運算符是new運算符的一種變體。通常new是在堆區來開辟內存的,如果需要可以new還可以在棧區和靜態區開辟空間。 要使用定位new運算符,要包含new頭文件#include<new>。 #include <iostream> #include <new>using namespace std;const int BUF = 512; const int N = 5; char buffer[BUF];int main() {double *p1, *p2;cout << "calling new and placement new " << endl;p1 = new double[N];p2 = new (buffer) double[N];for (int i = 0; i < N; i++){p2[i] = p1[i] = 1000 + 10.0*i;}cout << "heap:"<<p1<< " static:"<<(void*)buffer << endl;cout << "memory contents:" << endl;for (int i = 0; i < N;i++){cout <<" p1 "<< p1[i] << " at " << &p1[i] << "; ";cout << " p2 "<<p2[i] << " at " << &p2[i] << endl;}cout << " " << endl;cout << "calling new and placement new a second time" << endl;double *p3, *p4;p3 = new double[N];p4 = new (buffer) double[N];//重寫數據for (int i = 0; i < N; i++){p4[i] = p3[i] = 1000 + 50.0*i;}cout << "memory contents:" << endl;for (int i = 0; i < N; i++){cout << "p3 "<<p3[i] << " at " << &p3[i] << "; ";cout << "p4 " << p4[i] << " at " << &p4[i] << endl;}cout << " " << endl;cout << "calling new and placement new a third time" << endl;delete [] p1;p1 = new double[N];p2 = new(buffer + N*sizeof(double)) double[N];for (int i = 0; i < N; i++){p2[i] = p1[i] = 1000 + 70.0*i;}cout << "memory contents:" << endl;for (int i = 0; i < N; i++){cout <<"p1 "<< p1[i] << " at " << &p1[i] << "; ";cout << "p2"<<p2[i] << " at " << &p2[i] << endl;}delete[]p1;delete[]p3;cin.get();return 0; }
?
運行結果:
calling new and placement new heap:007AB2F8 static:01380320 memory contents:p1 1000 at 007AB2F8; p2 1000 at 01380320p1 1010 at 007AB300; p2 1010 at 01380328p1 1020 at 007AB308; p2 1020 at 01380330p1 1030 at 007AB310; p2 1030 at 01380338p1 1040 at 007AB318; p2 1040 at 01380340calling new and placement new a second time memory contents: p3 1000 at 007AB500; p4 1000 at 01380320 p3 1050 at 007AB508; p4 1050 at 01380328 p3 1100 at 007AB510; p4 1100 at 01380330 p3 1150 at 007AB518; p4 1150 at 01380338 p3 1200 at 007AB520; p4 1200 at 01380340calling new and placement new a third time memory contents: p1 1000 at 007AB2F8; p21000 at 01380348 p1 1070 at 007AB300; p21070 at 01380350 p1 1140 at 007AB308; p21140 at 01380358 p1 1210 at 007AB310; p21210 at 01380360 p1 1280 at 007AB318; p21280 at 01380368定位new運算符,將p2放在了數組buffer中,p2和buffer的地址都是01380320;然而他們的類型不同,p2是double的指針,buffer是char的指針。所以用void*對buffer進行類型轉換,不然buffer打印出來的是一個字符。
最后,不能直接delete [ ?] p2,由于buffer指定的是內存的靜態內存,而delete只能釋放堆區的分配的內存。
?
轉載于:https://www.cnblogs.com/wangpfcnblogs/p/4604311.html
總結
以上是生活随笔為你收集整理的C++的new和delete的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WPF基础学习笔记(一)Dependen
- 下一篇: 减少C++代码编译时间的方法