C++ メモリ管理:新規および削除
C++ メモリ管理:新規および削除
このチュートリアルでは、例の助けを借りて、新規および削除操作を使用して C++ で効果的にメモリを管理する方法を学習します。
C++ では、実行時に変数または配列のメモリを割り当てることができます。これは、動的メモリ割り当てとして知られています。
Java や Python などの他のプログラミング言語では、変数に割り当てられたメモリをコンパイラが自動的に管理します。しかし、これは C++ には当てはまりません。
C++ では、変数を使用しなくなった後、動的に割り当てられたメモリの割り当てを手動で解除する必要があります。
new
を使用して、メモリを動的に割り当ててから解放することができます と delete
C++ 新しい演算子
new
演算子はメモリを変数に割り当てます。たとえば、
// declare an int pointer
int* pointVar;
// dynamically allocate memory
// using the new keyword
pointVar = new int;
// assign value to allocated memory
*pointVar = 45;
ここでは、int
に動的にメモリを割り当てています。 new
を使用した変数
ポインタ pointVar を使用していることに注意してください 動的にメモリを割り当てます。これは new
演算子はメモリ位置のアドレスを返します。
配列の場合、new
演算子は、配列の最初の要素のアドレスを返します。
上記の例から、 new
を使用する構文が 演算子は
pointerVariable = new dataType;
オペレータを削除
動的に宣言した変数を使用する必要がなくなったら、変数が占有していたメモリの割り当てを解除できます。
このために、delete
演算子が使用されます。メモリをオペレーティング システムに返します。これはメモリ割り当て解除と呼ばれます .
この演算子の構文は
delete pointerVariable;
コードを検討してください:
// declare an int pointer
int* pointVar;
// dynamically allocate memory
// for an int variable
pointVar = new int;
// assign value to the variable memory
*pointVar = 45;
// print the value stored in memory
cout << *pointVar; // Output: 45
// deallocate the memory
delete pointVar;
ここでは、int
に対して動的にメモリを割り当てています。 ポインター pointVar を使用する変数 .
pointVar の内容を出力した後 、 delete
を使用してメモリの割り当てを解除しました .
注意 :プログラムが new
を使用して大量の不要なメモリを使用している場合 、オペレーティング システムで使用できるメモリがなくなるため、システムがクラッシュする可能性があります。この場合、delete
オペレーターはシステムのクラッシュを防ぐことができます。
例 1:C++ 動的メモリ割り当て
#include <iostream>
using namespace std;
int main() {
// declare an int pointer
int* pointInt;
// declare a float pointer
float* pointFloat;
// dynamically allocate memory
pointInt = new int;
pointFloat = new float;
// assigning value to the memory
*pointInt = 45;
*pointFloat = 45.45f;
cout << *pointInt << endl;
cout << *pointFloat << endl;
// deallocate the memory
delete pointInt;
delete pointFloat;
return 0;
}
出力
45 45.45
このプログラムでは、int
の 2 つの変数に動的にメモリを割り当てました。 と float
種類。それらに値を割り当てて出力した後、コードを使用して最終的にメモリの割り当てを解除します
delete pointInt;
delete pointFloat;
注: 動的メモリ割り当てにより、メモリ管理がより効率的になります。
特に配列の場合、実行時まで配列のサイズがわからないことがよくあります。
例 2:配列の C++ new および delete 演算子
// C++ Program to store GPA of n number of students and display it
// where n is the number of students entered by the user
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter total number of students: ";
cin >> num;
float* ptr;
// memory allocation of num number of floats
ptr = new float[num];
cout << "Enter GPA of students." << endl;
for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << ": ";
cin >> *(ptr + i);
}
cout << "\nDisplaying GPA of students." << endl;
for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << " :" << *(ptr + i) << endl;
}
// ptr memory is released
delete[] ptr;
return 0;
}
出力
Enter total number of students: 4 Enter GPA of students. Student1: 3.6 Student2: 3.1 Student3: 3.9 Student4: 2.9 Displaying GPA of students. Student1 :3.6 Student2 :3.1 Student3 :3.9 Student4 :2.9
このプログラムでは、ユーザーに生徒数を入力して num に保存するように求めています。
次に、float
にメモリを動的に割り当てました。 new を使用した配列 .
ポインター表記法を使用して配列にデータを入力します (後で出力します)。
配列が不要になったら、コード delete[] ptr;
を使用して配列メモリの割り当てを解除します .
[]
の使用に注意してください delete
の後 .角括弧 []
を使用します メモリの解放が配列のものであることを示すため。
例 3:オブジェクトの C++ new および delete 演算子
#include <iostream>
using namespace std;
class Student {
int age;
public:
// constructor initializes age to 12
Student() : age(12) {}
void getAge() {
cout << "Age = " << age << endl;
}
};
int main() {
// dynamically declare Student object
Student* ptr = new Student();
// call getAge() function
ptr->getAge();
// ptr memory is released
delete ptr;
return 0;
}
出力
Age = 12
このプログラムでは、Student
を作成しました。 プライベート変数 age を持つクラス .
年齢を初期化しました 12
へ デフォルトのコンストラクタ Student()
で 関数 getAge()
でその値を出力します .
main()
で 、 Student
を作成しました new
を使用したオブジェクト 演算子とポインター ptr を使用します そのアドレスを指します。
オブジェクトが作成された瞬間、Student()
コンストラクターは age を初期化します 12
へ .
次に getAge()
を呼び出します コードを使用した関数:
ptr->getAge();
アロー演算子 ->
に注意してください .この演算子は、ポインターを使用してクラス メンバーにアクセスするために使用されます。
C言語