在 C++ 中释放二维数组
本文将解释如何在 C++ 编程语言中初始化和释放二维数组。
可以用 C++ 创建的多维数组的最基本版本是二维数组; 矩阵是二维数组的另一个名称。 根据它的初始化方式,它可以是任何类型,包括整数、字符、浮点数等。
C++ 中的 new 运算符可以实现二维数组的动态分配,而 delete 运算符可以在数组使用后进行后续的释放。 在 new
运算符的帮助下分配二维数组时,有几种不同的方法可用。
当使用空闲存储分配动态内存时,您可以获得对已创建对象的引用; 但是,此指针指向已分配区域的第一个字节。
C++中二维数组的初始化
我们首先使用一个名为 arr 的二维数组创建一个数组数组,该数组包含五行两列。
int arr[5][2]={
{11, 12},
{13, 14},
{10, 12},
{14, 16},
{15, 17},
}
下面是另一种方法,我们可以用来建立一个五行两列的二维数组。
int arr[5][2] ={11, 12}
我们完成了一个二维数组的初始化过程。 尽管如此,由于我们还没有打印它,我们无法检查操作是否正确执行。
#include <iostream>
using namespace std;
int main()
{
int arr[5][2] = {
{11,12},
{13,14},
{10,12},
{14,16},
{15,17},
};
int i, j;
cout<<"The array is shown as follows:\n"<<endl;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 2; j++)
{
cout<<arr[i][j]<<"\t";
}
cout<<endl;
}
}
输出结果如下:
The array is shown as follows:
11 12
13 14
10 12
14 16
15 17
在 C++ 中使用 delete 运算符释放二维数组
释放数组是 C++ 中最重要的特性。
需要程序员干预才能释放动态分配的 RAM。 释放或释放数据变量、指针或数组占用的内存空间是通过删除运算符完成的。
在下面的示例中,我们将尝试为数组释放内存,它只将数组中的第一个整数设置为 0,其余保持不变。
在程序的 main()
方法中声明两个数据类型为 int 的变量 i 和 n。
int i, n;
现在是时候收集有关我们要存储的整数总数的输入了。
cout << "How many integers do you want to store?" << endl;
cin >> n;
我们将使用指针初始化一个名为 pointerArray 的 int 类型数组。
int* pointerArray = new int[n];
让用户提供一些输入,以便我们可以用一些数字填充数组。 我们使用 for 循环迭代并接受输入,直到它的值等于我们希望存储在系统中的整数数量。
for (int i = 0; i < n; i++) {
cout << endl;
cout << "Please input an integer\n";
cin >> pointerArray[i];
}
我们将打印出现在保存在数组中的整数。 为此,我们使用 for 循环。
cout << "Stored array values\n";
for (int i = 0; i < n; i++) {
cout << pointerArray[i] << endl;
}
要释放数组,您必须首先使用 delete[]
运算符。 我们再次打印数组以确认它已被有效释放。
cout << "Deallocating array\n";
delete[] pointerArray;
cout << "Print the array to check if the array is deallocated\n";
for (i = 0; i < n; i++) {
cout << pointerArray[i] << endl;
}
完整代码如下
#include<iostream>
using namespace std;
int main()
{
int i, n;
cout << "How many integers do you want to store?" << endl;
cin >> n;
int* pointerArray = new int[n];
for (int i = 0; i < n; i++){
cout << endl;
cout << "Please input an integer\n";
cin >> pointerArray[i];
}
cout << "Stored array values\n";
for (int i = 0; i < n; i++){
cout << pointerArray[i] << endl;
}
cout << "Deallocating array\n";
delete[] pointerArray;
cout << "Print the array to check if the array is deallocated\n";
for (i = 0; i < n; i++) {
cout << pointerArray[i] << endl;
}
}
输出结果如下:
How many integers do you want to store?
2
Please input an integer
2
Please input an integer
3
Stored array values
2
3
Deallocating array
Print the array to check if the array is deallocated
0
0
在 C++ 中使用 malloc() 和 free() 运算符释放二维数组
在本例中,我们使用 malloc()
函数将大小为 0 的 int 内存分配给 newPointer 指针。
int* newPointer = (int*) malloc(0);
之后,我们使用 if 语句来确定 malloc()
是否提供了地址或空指针。 如果它产生了一个地址,我们继续下一步。
if (newPointer==NULL) {
cout << "Null value";
}
else {
cout << "Address = " << newPointer;
}
最终,我们使用释放方法 free()
释放了内存。
free(newPointer);
完整代码如下:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int *newPointer = (int*) malloc(0);
if (newPointer==NULL) {
cout << "Null value";
}
else {
cout << "Address = " << newPointer;
}
free(newPointer);
return 0;
}
输出结果:
Address = 0x56063cf64eb0
相关文章
Arduino 复位
发布时间:2024/03/13 浏览次数:315 分类:C++
-
可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。
Arduino 的字符转换为整型
发布时间:2024/03/13 浏览次数:181 分类:C++
-
可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。
Arduino 串口打印多个变量
发布时间:2024/03/13 浏览次数:381 分类:C++
-
可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。