2016-12-21 67 views
4

有人能帮我理解这段代码的最后一行吗?我不明白如何动态分配二维数组。我知道在第6行中,它创建了一个名为'a'的指针,并将其指向由'c'定义的大小为5的整数数组。了解具有动态内存分配的2D阵列

我不明白的是,“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 
} 
+0

您的第一段的最后一项是错误的。 'a'被声明为一个指向数组5'int'的指针; *而已*。它没有指向任何东西*(还)。这就是'new []'的用途。 – WhozCraig

+0

二维数组是指向指向数组的指针,每个指向另一个数组(在您的情况下为整数)。 – Ripi2

+0

你忘记了'int r = 5'后的分号。 – NonCreature0714

回答

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点数组的第一个元素。

由于Tint [M]可以写

int (*a)[M] = new int [N][M]; 

即此语句分配类型int[M]N元件和指针a的阵列获取所分配的阵列的第一个元素的地址等效TP。

返回到你的程序示例

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]类型的r元素(对象)的数组而a是指针到分配数组的第一个元素。

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