通过动态内存分配了解2D数组

Understanding 2D array with dynamic memory allocation

本文关键字:了解 2D 数组 分配 内存 动态      更新时间:2023-10-16

有人可以帮助我了解此代码段中的最后一行吗?我不明白这是如何动态分配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]类型和ar元素(对象)的数组是指向分配数组的第一个元素的指针。

您只是将指针用于一维数组,这需要两个索引来访问元素。没什么疯狂的,不是完全不确定的行为,但也不好。

#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