通过动态内存分配了解2D数组
Understanding 2D array with dynamic memory allocation
有人可以帮助我了解此代码段中的最后一行吗?我不明白这是如何动态分配2D数组的。我了解到,在第6行中,它创建了一个名为" A"的指针,并将其指向" C"定义的大小5的整数。
。我不明白的是"新INT"语句如何与将R插入方程式中的r一起使用。预先感谢。
#include <iostream>
const int c = 5; //num of columns
int main () {
int r = 5;
int (*a)[c];
a = new int[r][c]; // allocate array
}
如果您有类型T
,并且将分配大小N
的数组,则此表达式
new T[N]
返回分配数组第一个元素的类型T *
的地址。
所以你应该写
T *a = new T[N];
例如,如果 T
等于int类型
typedef int T;
或
using T = int;
然后可以写上述分配
int *a = new int[N];
数组元素的大小等于 sizeof( int )
,通常为4个字节。
现在,假设您将分配int[M]
类型元素的数组,其中M
是一个恒定的整数表达式。
您可以写
typedef int T[M];
或
using T = int[M];
和
T *a = new T[N];
因此,您分配了一个N
元素的数组,其中每个元素的大小sizeof( int[M]
,指针a
指向数组的第一个元素。
因为T
是等效的TP int [M]
您可以写
int ( *a )[M] = new int [N][M];
这是此语句分配类型int[M]
的N
元素和指针a
获取分配数组的第一个元素的地址。
返回您的程序示例
int r = 5 int(*a)[c]; a = new int [r] [c];
您可以以以下方式重写
typedef int T[c];
或
using T = int[c];
和
T *a = new T[r];
这是此语句分配了int[c]
类型和a
的r
元素(对象)的数组是指向分配数组的第一个元素的指针。
您只是将指针用于一维数组,这需要两个索引来访问元素。没什么疯狂的,不是完全不确定的行为,但也不好。
#include <iostream>
const int c = 5; //num of columns
int main () {
int r = 5;
//Creates a pointer to an array of 5 integer pointers.
int (*a)[c];
a = new int[r][c]; // allocate array
std::cout << *a << std::endl;
std::cout << a << std::endl;
std::cout << std::endl;
std::cout << "Displaying deferenced, unallocated array." << std::endl;
for(int i=0; i<c;++i){
std::cout << *a[i] << std::endl;
}
std::cout << "Diplaying pointers in the array." << std::endl;
std::cout << "Note how it's not a 2d array." << std::endl;
for(int i=0;i<c;++i){
for(int j=0;j<r;++j){
std::cout << a[i] << " ";
}
std::cout << std::endl;
}
std::cout << "Allocating array 1d, 1d style fails..." << std::endl;
/*
for(int i=0;i<c;++i){
a[i] = 23; //Syntax error! Requires two indexes.
}
*/
std::cout << "Allocating 1d array 2d style... success!?" << std::endl;
for(int i=0;i<r;++i){
for(int j=0;j<c;++j){
a[i][j] = 13;
}
}
std::cout << "Displaying allocated array." << std::endl;
for(int i=0;i<r;++i){
for(int j=0;j<c;++j){
std::cout << a[i][j] << " ";
}
std::cout << std::endl;
}
delete [] a;
}
输出:
0x100202ba0
0x100202ba0
Displaying deferenced, unallocated array.
0
0
0
0
0
Diplaying pointers in the array.
Note how it's not a 2d array.
0x100202ba0 0x100202ba0 0x100202ba0 0x100202ba0 0x100202ba0
0x100202bb4 0x100202bb4 0x100202bb4 0x100202bb4 0x100202bb4
0x100202bc8 0x100202bc8 0x100202bc8 0x100202bc8 0x100202bc8
0x100202bdc 0x100202bdc 0x100202bdc 0x100202bdc 0x100202bdc
0x100202bf0 0x100202bf0 0x100202bf0 0x100202bf0 0x100202bf0
Allocating array 1d, 1d style fails...
Allocating 1d array 2d style... success!?
Displaying allocated array.
13 13 13 13 13
13 13 13 13 13
13 13 13 13 13
13 13 13 13 13
13 13 13 13 13
Program ended with exit code: 0
相关文章:
- 2D数组来自文本输入,中间有空格
- 将值指定给向量(2D)的向量中的某个位置
- 正在尝试了解输入验证循环
- 如何使用用户输入在C++中正确填充2D数组
- 了解 GLM- openGL 中的相机转换
- C++我需要了解在哪里使用指针和双指针
- 如何深入了解明显的腐败
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 四边形的 2D 旋转
- 打印第二列时的2d字符矢量打印空间
- 了解嵌套循环打印星号图案
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- 了解每月第一天函数的代码
- 了解2D阵列的副本
- 通过动态内存分配了解2D数组