C++中malloc與free、new與delete的詳解與應(yīng)用
更新時間:2018年12月21日 14:56:46 作者:蝸牛201
今天小編就為大家分享一篇關(guān)于C++中malloc與free、new與delete的詳解與應(yīng)用,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
C++面試經(jīng)常會問到關(guān)于malloc/free和new/delete的區(qū)別,網(wǎng)上有不同版本的解釋,這里總結(jié)下并加上個人理解和使用。
兩者相同點(diǎn)
- 1.都可以申請動態(tài)堆內(nèi)存。
兩者不同點(diǎn)
- 1.new/delete是C++的操作符,malloc/free是C/C++的標(biāo)準(zhǔn)庫函數(shù)。
- 2.new申請的可以理解為對象,new時會調(diào)用構(gòu)造函數(shù),返回指向該對象的指針,delete時調(diào)用析構(gòu)函數(shù);malloc申請的只是內(nèi)存,不是對象。
- 3.new/delete是保留字,不需要頭文件支持;malloc/free需要頭文件庫函數(shù)支持。
注意事項
- 1.用new申請的內(nèi)存,必須用delete釋放。
- 2.用new[]申請的內(nèi)存,必須用delete[]釋放。
- 3.delete釋放內(nèi)存后,指針值不變,良好的風(fēng)格是釋放后指針置為NULL,如,delete p; p = NULL。
- 4.用malloc申請的內(nèi)存,必須用free釋放。
使用
#include "stdafx.h"
#include <stdio.h>
#include "stdlib.h"
#include <string.h>
struct Stu
{
char name[32];
int age;
};
int main()
{
/**************************** 基本用法 **********************************/
//申請一個int類型
int *p1 = new int; //直接申請賦值 int* p1 = new int(3);
int *p2 = (int*)malloc(sizeof(int));
//申請一個char類型
char *p3 = new char; //直接申請賦值 char *p3 = new char('c');
char *p4 = (char*)malloc(sizeof(char));
//申請一個int型一維數(shù)組
int *p5 = new int[5]; //直接申請賦值 int *p5 = new int[5]{1,2,3,4,5};
int *p6 = (int*)malloc(sizeof(int)*5);
//申請一個char型一維數(shù)組
char* p7 = new char[6]; //直接申請賦值 char* p7 = new char[3]{'a', 'v', 'c'};
char* p8 = (char*)malloc(sizeof(char)*6);
//申請一個int型二維數(shù)組
int(*p9)[2] = new int[2][2]; //直接申請賦值 int(*p9)[2] = new int[2][2]{ 1,2,3,4 };
int(*p10)[2] = (int(*)[2])malloc(sizeof(int)*2*2);
//申請一個char型二維數(shù)組
char(*p11)[2] = new char[2][2];
char(*p12)[2] = (char(*)[2])malloc(sizeof(char)*2*2);
/***************************** 申請二級指針內(nèi)存 **********************************/
//申請二級指針(new,delete)
char** p13 = new char*[2];
p13[0] = "aaaaaaaaaa";
p13[1] = "vvvvvvvvvv";
delete p13;
//申請二級指針(malloc, free)
char** p14 = (char**)malloc(sizeof(char*)*2);
p14[0] = "cccccccc";
p14[1] = "dddddddd";
delete p14;
/****************************** 申請結(jié)構(gòu)體內(nèi)存 *********************************/
//new delete
Stu* pStu1 = new Stu;
Stu* pStu2 = new Stu{"wpf", 10};
Stu* pStu3 = new Stu[1024];
delete pStu1;
delete pStu2;
delete[] pStu3;
//malloc free
Stu* pStu4 = (Stu*)malloc(sizeof(Stu));
memset(pStu4, 0, sizeof(Stu));
free(pStu4);
getchar();
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
C++ Boost實現(xiàn)數(shù)字與字符串轉(zhuǎn)化詳解
Boost是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱。Boost庫是一個可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱2022-11-11
C語言深入講解動態(tài)內(nèi)存分配函數(shù)的使用
這篇文章主要介紹了C語言動態(tài)內(nèi)存分配,C語言內(nèi)存管理相關(guān)的函數(shù)主要有realloc、calloc、malloc、free、柔性數(shù)組等,下面這篇文章帶大家了解一下2022-05-05
C++ 中的虛函數(shù)表及虛函數(shù)執(zhí)行原理詳解
這篇文章主要介紹了C++ 中的虛函數(shù)表及虛函數(shù)執(zhí)行原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

