介紹

成都創(chuàng)新互聯(lián)成立與2013年,先為黎川等服務(wù)建站,黎川等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為黎川企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
1.malloc,free和new,delete區(qū)別。
2.使用new遵循原則:
使用
1.申請一個對象
int* p1 = new int; delete p1; p1 = NULL;
2.申請多個對象
int* p1 = new int[12]; delete[] p1; p1 = NULL;
3.申請一個長度為1024的char數(shù)組
char* pArray = new char[1024];
for (int i=0; i < 1024; i++)
{
pArray[i] = i;
}
delete[] pArray;
pArray = NULL;4.申請一個類對象
#include <stdio.h>
class Student
{
public:
char name[32];
int age;
};
int main()
{
Student* pStu = new Student();
delete pStu;
pStu = NULL;
return 1;
}5.申請1024個類對象
#include <stdio.h>
class Student
{
public:
int age;
Student()
{
...
}
~Student()
{
...
}
};
int main()
{
Student* pStu = new Student[1024];
for (int i=0; i<1024; i++)
{
pStu[i].age = i+1;
}
delete[] pStu;
pStu = NULL;
return 1;
}new多個對象不能傳參數(shù),要求該類必須有默認(rèn)構(gòu)造函數(shù)。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對創(chuàng)新互聯(lián)的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
當(dāng)前文章:C++中new和delete的介紹
文章鏈接:http://www.chinadenli.net/article32/gspjsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、小程序開發(fā)、移動網(wǎng)站建設(shè)、面包屑導(dǎo)航、網(wǎng)站設(shè)計(jì)公司、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)