了解具有动态内存分配的二维数组
Understanding 2D array with dynamic memory allocation
有人可以帮我理解这段代码的最后一行吗?我不明白它是如何动态分配二维数组的。我知道在第 6 行,它创建了一个名为 'a' 的指针,并将其指向由 'c'.
定义的大小为 5 的整数数组
我不明白 "new int" 语句是如何与等式中的 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
}
你只是在创建一个指向一维数组的指针,它需要两个索引来访问元素。没什么疯狂的,也不是完全未定义的行为,但也不好。
#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
如果你有一个类型 T
并且要分配一个大小为 N
的数组,那么这个表达式
new T[N]
returns分配数组第一个元素的类型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
等价于tpint [M]
你可以写
int ( *a )[M] = new int [N][M];
即此语句分配 int[M]
类型的 N
个元素的数组,指针 a
获取分配数组的第一个元素的地址。
返回您的程序示例
int r = 5
整数 (*a)[c];
a = new int[r][c];
你可以改写成下面的方式
typedef int T[c];
或
using T = int[c];
和
T *a = new T[r];
即此语句分配 r
类型 int[c]
元素(对象)的数组,a
是指向已分配数组第一个元素的指针。
有人可以帮我理解这段代码的最后一行吗?我不明白它是如何动态分配二维数组的。我知道在第 6 行,它创建了一个名为 'a' 的指针,并将其指向由 'c'.
定义的大小为 5 的整数数组我不明白 "new int" 语句是如何与等式中的 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
}
你只是在创建一个指向一维数组的指针,它需要两个索引来访问元素。没什么疯狂的,也不是完全未定义的行为,但也不好。
#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
如果你有一个类型 T
并且要分配一个大小为 N
的数组,那么这个表达式
new T[N]
returns分配数组第一个元素的类型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
等价于tpint [M]
你可以写
int ( *a )[M] = new int [N][M];
即此语句分配 int[M]
类型的 N
个元素的数组,指针 a
获取分配数组的第一个元素的地址。
返回您的程序示例
int r = 5 整数 (*a)[c]; a = new int[r][c];
你可以改写成下面的方式
typedef int T[c];
或
using T = int[c];
和
T *a = new T[r];
即此语句分配 r
类型 int[c]
元素(对象)的数组,a
是指向已分配数组第一个元素的指针。