C/C++ における動的メモリ割り当てとは、プログラマが手動でメモリ割り当てを実行することを指します。動的に割り当てられるメモリは、 ヒープ、 非静的変数とローカル変数にはメモリが割り当てられます。 スタック (参照する メモリレイアウト C プログラム 詳細については)。
アプリケーションとは何ですか?
- 動的に割り当てられたメモリの使用法の 1 つは、可変サイズのメモリを割り当てることですが、これはコンパイラによって割り当てられたメモリでは不可能です。 可変長配列 。
- 最も重要な用途は、プログラマーに提供される柔軟性です。メモリが必要になったときも、不要になったときも、自由にメモリの割り当てと割り当て解除を行うことができます。この柔軟性が役立つケースは数多くあります。そのようなケースの例としては、 木 、など。
通常の変数に割り当てられるメモリとどう違うのでしょうか?
int a、char str[10] などの通常の変数の場合、メモリは自動的に割り当てられ、割り当て解除されます。 int *p = new int[10] のような動的に割り当てられたメモリの場合、必要がなくなったときにメモリの割り当てを解除するのはプログラマの責任です。プログラマがメモリの割り当てを解除しないと、 メモリーリーク (プログラムが終了するまでメモリの割り当ては解除されません)。
C++ ではメモリの割り当て/割り当て解除はどのように行われますか?
C は、 malloc() と calloc() 関数は実行時に動的にメモリを割り当て、free() 関数を使用して動的に割り当てられたメモリを解放します。 C++ はこれらの関数をサポートしており、2 つの演算子もあります 新しい そして 消去、 メモリの割り当てと解放のタスクをより適切かつ簡単な方法で実行します。
新しいオペレーター
new 演算子は、Free Store でのメモリ割り当ての要求を示します。十分なメモリが利用可能な場合、new オペレータはメモリを初期化し、新しく割り当てられ初期化されたメモリのアドレスをポインタ変数に返します。
new 演算子を使用する構文
スクリプトを実行可能にする
pointer-variable = new data-type;>
ここで、ポインタ変数は data-type 型のポインタです。データ型は、配列を含む任意の組み込みデータ型、または構造体やクラスを含む任意のユーザー定義データ型にすることができます。
例:
// Pointer initialized with NULL // Then request memory for the variable int *p = NULL; p = new int; OR // Combine declaration of pointer // and their assignment int *p = new int;>C++
// C++ program to demonstrate how to create dynamic variable // using new #include #include using namespace std; int main() { // pointer to store the address returned by the new int* ptr; // allocating memory for integer ptr = new int; // assigning value using dereference operator *ptr = 10; // printing value and address cout << 'Address: ' << ptr << endl; cout << 'Value: ' << *ptr; return 0; }> 出力
Address: 0x162bc20 Value: 10>
メモリを初期化します。 new 演算子を使用して、組み込みデータ型のメモリを初期化することもできます。カスタム データ型の場合、値を初期化するために (データ型を入力として) コンストラクターが必要です。両方のデータ型の初期化の例を次に示します。
pointer-variable = new data-type(value);>
例:
C++ // C++ program to illustrate how to initialize a dynamic // variable with allocation #include #include using namespace std; // Custom data type with constructor to take initial value struct cust { int p; cust(int q) : p(q) { } cust() = default; }; int main() { // creating inbuit data types with initial value int* p = new int(25); float* q = new float(75.25); // Works fine, doesn’t require constructor cust* var1 = new cust; // OR // Works fine, doesn’t require constructor var1 = new cust(); // Notice error if you comment this line cust* var = new cust(25); cout << *p << ' ' << *q << ' ' << var->p; 0を返します。 }>> 出力
25 75.25 25>
メモリのブロックを割り当てます。 new 演算子は、次のタイプのメモリのブロック (配列) を割り当てるためにも使用されます。 データ・タイプ 。
pointer-variable = new data-type[size];>
ここで、size(変数) は配列内の要素の数を指定します。
例:
JavaScriptスリープ
int *p = new int[10]>
int 型の連続する 10 個の整数にメモリを動的に割り当て、top(ポインタ) が割り当てられたシーケンスの最初の要素へのポインタを返します。 p[0] は最初の要素を指し、p[1] は 2 番目の要素を指します。

通常の配列宣言と new の使用
通常の配列を宣言することと、new を使用してメモリのブロックを割り当てることには違いがあります。最も重要な違いは、通常の配列はコンパイラによって割り当てが解除されることです (配列がローカルの場合は、関数が返されるか完了するときに割り当てが解除されます)。ただし、動的に割り当てられた配列は、プログラマによって割り当てが解除されるか、プログラムが終了するまで、常にそこに残ります。
scan.next Java
実行時に十分なメモリが利用できない場合はどうすればよいでしょうか?
割り当てるための十分なメモリがヒープ内にない場合、新しいリクエストはタイプ std::bad_alloc の例外をスローすることで失敗を示します。ただし、 new 演算子で nothrow が使用されている場合は除きます。この場合、NULL ポインタが返されます (「例外」セクションまでスクロール)での新しい演算子の処理 これ 記事)。したがって、new のプログラムを使用する前に、new によって生成されたポインタ変数を確認することをお勧めします。
int *p = new(nothrow) int; if (!p) { cout << 'Memory allocation failed
'; }>削除演算子
動的に割り当てられたメモリの割り当てを解除するのはプログラマの責任であるため、プログラマには C++ 言語の削除演算子が提供されます。
構文:
// Release memory pointed by pointer-variable delete pointer-variable;>
ここで、ポインタ変数は、によって作成されたデータ オブジェクトを指すポインタです。 新しい 。
例:
delete p; delete q;>
ポインタ変数が指す動的に割り当てられた配列を解放するには、次の形式を使用します。 消去 :
// Release block of memory // pointed by pointer-variable delete[] pointer-variable; Example: // It will free the entire array // pointed by p. delete[] p;>CPP
// C++ program to illustrate dynamic allocation // and deallocation of memory using new and delete #include using namespace std; int main() { // Pointer initialization to null int* p = NULL; // Request memory for the variable // using new operator p = new (nothrow) int; if (!p) cout << 'allocation of memory failed
'; else { // Store value at allocated address *p = 29; cout << 'Value of p: ' << *p << endl; } // Request block of memory // using new operator float* r = new float(75.25); cout << 'Value of r: ' << *r << endl; // Request block of memory of size n int n = 5; int* q = new (nothrow) int[n]; if (!q) cout << 'allocation of memory failed
'; else { for (int i = 0; i < n; i++) q[i] = i + 1; cout << 'Value store in block of memory: '; for (int i = 0; i < n; i++) cout << q[i] << ' '; } // freed the allocated memory delete p; delete r; // freed the block of allocated memory delete[] q; return 0; }> 出力
Value of p: 29 Value of r: 75.25 Value store in block of memory: 1 2 3 4 5>
時間計算量: O(n)、n は指定されたメモリ サイズです。
関連記事:
- 新規と削除に関するクイズ
- 削除と無料