C++ 内存管理:新建和删除
C++ 内存管理:新建和删除
在本教程中,我们将通过示例学习在 C++ 中使用 new 和 delete 操作有效地管理内存。
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
的两个变量分配内存 和 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 的类 .
我们已经初始化了 age 到 12
在默认构造函数 Student()
并使用函数 getAge()
打印其值 .
在 main()
,我们创建了一个Student
使用 new
的对象 运算符并使用指针 ptr 指向它的地址。
创建对象的那一刻,Student()
构造函数初始化 age 到 12
.
然后我们调用 getAge()
函数使用代码:
ptr->getAge();
注意箭头运算符 ->
.该运算符用于使用指针访问类成员。
C语言