2016-12-21 68 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