What is the difference between new
/delete
and malloc
/free
?
Related (duplicate?): In what cases do I use malloc vs new?
What is the difference between new
/delete
and malloc
/free
?
Related (duplicate?): In what cases do I use malloc vs new?
new
/delete
new
(標準バージョン) はNULL
(失敗時にスローされます) を返しません。malloc
/を呼び出すかどうかfree
は実装定義です。std::set_new_handler
)。operator new
/operator delete
は合法的にオーバーライドできます。malloc
/free
void*
。NULL
。new
delete
malloc
/合法的に無効にfree
することはできません。特徴 | new /delete |
malloc /free |
---|---|---|
から割り当てられたメモリ | 「フリーストア」 | 'ヒープ' |
戻り値 | 完全に型指定されたポインター | void* |
失敗時 | 投げる (返さないNULL ) |
戻り値NULL |
必要なサイズ | コンパイラによる計算 | バイト単位で指定する必要があります |
配列の処理 | 明示的なバージョンがあります | 手計算が必要 |
再割り当て中 | 直感的に扱えない | シンプル (コピー コンストラクターなし) |
逆転の呼び声 | 実装定義 | いいえ |
メモリ不足のケース | 新しいメモリ アロケータを追加できます | ユーザーコードで処理されない |
上書き可能 | はい | いいえ |
コンストラクタ/デストラクタの使用 | はい | いいえ |
技術的には、によって割り当てられたメモリnew
は「フリーストア」から取得され、によって割り当てられたメモリmalloc
は「ヒープ」から取得されます。これら 2 つの領域が同じかどうかは実装の詳細であり、これが混在できないもう 1 つmalloc
の理由です。new
The most relevant difference is that the new
operator allocates memory then calls the constructor, and delete
calls the destructor then deallocates the memory.
new
calls the ctor of the object, delete
call the dtor.
malloc
& free
just allocate and release raw memory.
new
/delete
is C++, malloc
/free
comes from good old C.
In C++, new
calls an objects constructor and delete
calls the destructor.
malloc
and free
, coming from the dark ages before OO, only allocate and free the memory, without executing any code of the object.
In C++ new
/delete
call the Constructor/Destructor accordingly.
malloc
/free
simply allocate memory from the heap. new
/delete
allocate memory as well.
new と malloc の主な違いは、new がオブジェクトのコンストラクタを呼び出し、対応する delete の呼び出しがオブジェクトのデストラクタを呼び出すことです。
他にも違いがあります:
new
タイプセーフであり、malloc
タイプのオブジェクトを返しますvoid*
new
エラー時に例外をスローし、errnoをmalloc
返して設定しますNULL
new
は演算子であり、オーバーロードできます。malloc
は関数であり、オーバーロードできません
new[]
配列を割り当てる は、より直感的で型安全ですmalloc
malloc
-派生割り当ては を介してサイズ変更できますがrealloc
、 -new
派生割り当てはサイズ変更できません
malloc
Nバイトのメモリチャンクを割り当てることができますnew
。たとえば、char
型の配列を割り当てるように要求する必要があります
違いを見てみると、malloc は C 風、new は C++ 風です。コードベースに適したものを使用してください。
new と malloc を異なるメモリ割り当てアルゴリズムを使用して実装することは合法ですが、ほとんどのシステムでは、new は malloc を使用して内部的に実装されるため、システム レベルの違いは生じません。
唯一の類似点は、malloc
どちらnew
もヒープ上の一部のメモリをアドレス指定するポインタを返し、そのようなメモリ ブロックが返されると、最初に解放/削除しない限り、再び返されないことを保証することです。つまり、どちらもメモリを「割り当て」ます。
ただし、new
/delete
は、コンストラクタ、デストラクタ、および演算子のオーバーロードを介して、さらに任意の他の作業を実行します。malloc
/free
メモリの割り当てと解放のみを行います。
実際、new
ヒープからメモリを返す必要がなく、メモリをまったく割り当てなくても十分にカスタマイズできます。ただし、デフォルトnew
はそうです。
また、
グローバルなnewとdeleteはオーバーライドできますが、malloc/freeはオーバーライドできません。
さらに多くのnewとdeleteをタイプごとにオーバーライドできます。
new
and delete
are C++ primitives which declare a new instance of a class or delete it (thus invoking the destructor of the class for the instance).
malloc
and free
are C functions and they allocate and free memory blocks (in size).
Both use the heap to make the allocation. malloc
and free
are nonetheless more "low level" as they just reserve a chunk of memory space which will probably be associated with a pointer. No structures are created around that memory (unless you consider a C array to be a structure).
malloc()
必要がありますが、これは には必要ありません。 <stdlib.h>
<alloc.h>
new
new
オーバーロードすることはできますが、delete
オーバーロードするmalloc
ことはできません。new
メモリを割り当てたいアドレスを渡すことができますが、malloc
.このコードは、delete キーワードまたは free 関数を使用するためのものです。ただし、「malloc」または「new」を使用してポインター オブジェクトを作成し、delete を使用してオブジェクト メモリの割り当てを解除すると、そのオブジェクト ポインターでもクラス内の関数を呼び出すことができます。その後、delete の代わりに free を使用すると、free ステートメントの後にも機能しますが、両方を使用すると、ポインター オブジェクトのみがクラス内の関数を呼び出すことができません。コードは次のとおりです。
#include<iostream>
using namespace std;
class ABC{
public: ABC(){
cout<<"Hello"<<endl;
}
void disp(){
cout<<"Hi\n";
}
};
int main(){
ABC* b=(ABC*)malloc(sizeof(ABC));
int* q = new int[20];
ABC *a=new ABC();
b->disp();
cout<<b<<endl;
free(b);
delete b;
//a=NULL;
b->disp();
ABC();
cout<<b;
return 0;
}
出力:
Hello Hi 0x2abfef37cc20