C++的使用小教程9——动态内存
学习好幸苦。
1、什么是动态内存
C++编程中内存可以被分为两部分,分别是栈和堆。
栈:在函数内部所声明的所有变量使用的都是栈空间。
堆:其为程序中未使用的所有空间,可用于动态分配。
使用特定的运算符可以进行动态内存分配。
利用new运算符可以实现动态内存分配。
使用delete运算符可以删除之前分配的动态内存。
2、实现方式
在C++中可以通过如下方式实现动态内存的分配。
type_name* element= new class_name;
以int为例,具体应用方式为:
int* test = new int;
*int = 5;
在C++中可以通过如下方式删除之前分配的动态内存。
delete element;
以int为例,具体的应用方式为:
delete test;
3、应用实例
3.1、单变量与对象的动态内存分配
接下来的例子讲述的是如何实现单变量与对象的动态内存分配。
#include <iostream>
using namespace std;
class Shape
{
public:
// 构造函数定义
Shape(double l = 2.0, double b = 2.0)
{
length = l;
breadth = b;
}
// 析构函数定义
~Shape()
{
cout << "The class is end" << endl;
}
void setLength(double lengthIn) {
length = lengthIn;
}
void setBreadth(double breadthIn) {
breadth = breadthIn;
}
virtual double getArea() = 0;
protected:
double length; // 长度
double breadth; // 宽度
};
// 圆形
class Circle :public Shape {
public:
Circle(double l = 2.0, double b = 2.0)
{
length = l;
breadth = b;
}
double getArea() {
return 3.1415926*length*breadth / 4;
}
};
// 长方形
class Rectangle :public Shape {
public:
Rectangle(double l = 2.0, double b = 2.0)
{
length = l;
breadth = b;
}
double getArea() {
return length * breadth;
}
};
int main(void)
{
// 单变量部分
int* test = new int;
*test = 5;
cout << "单变量的动态内存分配" << endl;
cout << "The int is " << *test << endl;
cout << endl;
// 对象部分
Circle* circle = new Circle(2,2);
Rectangle* rectangle = new Rectangle(3,4);
cout << "对象的动态内存分配" << endl;
cout << "The area of circle is " << circle->getArea() << endl;
cout << "The area of rectangle is " << rectangle->getArea() << endl;
// 删除之前定义的动态内存
delete test;
delete circle;
delete rectangle;
system("pause");
return 0;
}
应用结果为:
单变量的动态内存分配
The int is 5
对象的动态内存分配
The area of circle is 3.14159
The area of rectangle is 12
The class is end
The class is end
请按任意键继续. . .
3.2、数组的动态内存分配
3.2.1、一维数组的动态内存分配
#include <iostream>
using namespace std;
int main(void)
{
int* arr = new int[5];
for (int i = 0; i < 5; i++) {
arr[i] = i;
}
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;
delete[] arr;
system("pause");
}
应用结果为:
0 1 2 3 4
请按任意键继续. . .
3.2.2、二维数组的动态内存分配
#include <iostream>
using namespace std;
int main(void)
{
// 动态内存分配
int** arr;
arr = new int*[5];
for (int i = 0; i < 5; i++) {
arr[i] = new int[4];
}
// 赋值
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
arr[i][j] = i * 5 + j;
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
// 删除动态内存
for (int i = 0; i < 5; i++) {
delete[] arr[i];
}
delete[] arr;
system("pause");
}
应用结果为:
0 1 2 3
5 6 7 8
10 11 12 13
15 16 17 18
20 21 22 23
请按任意键继续. . .
3.2.3、三维数组的动态内存分配
#include <iostream>
using namespace std;
int main(void)
{
// 动态内存分配
int*** arr;
arr = new int**[2];
for (int i = 0; i < 2; i++) {
arr[i] = new int*[3];
for (int j = 0; j < 3; j++) {
arr[i][j] = new int[4];
}
}
// 赋值
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
arr[i][j][k] = i * 3 * 4 + j * 4 + k;
}
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
cout << arr[i][j][k] << " ";
}
}
cout << endl;
}
// 删除动态内存
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
delete[] arr[i][j];
}
delete[] arr[i];
}
delete[] arr;
system("pause");
}
应用结果为:
0 1 2 3 4 5 6 7 8 9 10 11
12 13 14 15 16 17 18 19 20 21 22 23
请按任意键继续. . .