了解具有动态内存分配的2D数组

时间:2016-12-21 15:45:08

标签: c++ arrays dynamic-memory-allocation

有人可以帮我理解这段代码中的最后一行吗?我不明白如何动态分配2D数组。据我所知,在第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
}

2 个答案:

答案 0 :(得分:2)

如果你有一个类型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]; 

即此语句分配N类型的int[M]元素数组,指针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];

这个语句分配一个类型为r的{​​{1}}元素(对象)数组,而int[c]是指向已分配数组的第一个元素的指针。

答案 1 :(得分:0)

你只是制作一个指向一维数组的指针,这需要两个索引才能访问元素。没有什么是疯狂的,也没有完全不明确的行为,但也不是很好。

#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