Declaring a pointer to multidimensional array and allocating the array

前端 未结 6 404
名媛妹妹
名媛妹妹 2020-12-02 10:32

I\'ve tried looking but I haven\'t found anything with a definitive answer. I know my problem can\'t be that hard. Maybe it\'s just that I\'m tired..

Basically, I wa

相关标签:
6条回答
  • 2020-12-02 10:35

    A ready to use example from here, after few seconds of googling with phrase "two dimensional dynamic array":

    int **dynamicArray = 0;
    
    // memory allocated for elements of rows. 
    dynamicArray = new int *[ROWS];
    
    // memory allocated for  elements of each column.  
    for( int i = 0 ; i < ROWS ; i++ ) {
        dynamicArray[i] = new int[COLUMNS];
    }
    
    // free the allocated memory 
    for( int i = 0 ; i < ROWS ; i++ ) {
        delete [] dynamicArray[i];
    }
    delete [] dynamicArray;
    
    0 讨论(0)
  • 2020-12-02 10:40

    Personally, my preference is to use a syntactic trick to declare a pointer to the dynamically sized multi-dimensional array. This works in compilers that support Variable Length Arrays (VLAs), which all C++ compilers should, and most current C compilers.

    The basic idea is captured in this:

    void bar (int *p, int nz, int ny, int nx) {
      int (*A)[ny][nx] = (int(*)[ny][nx]) p;
    

    "p" points at the (contiguous) block of space you want to treat as a multi-dimensional array. "A" has the same value as "p", but the declaration makes the compiler treat references to "A" in the multi-dimensional way you want. For example:

    #include <iostream>
    using namespace std;
    
    void bar (int *p, int nz, int ny, int nx)
    {
      int (*A)[ny][nx] = (int(*)[ny][nx]) p;
    
      for (int ii = 0; ii < nz; ii++) {
        for (int jj = 0; jj < ny; jj++) {
          for(int kk = 0; kk < nx; kk++) {
              A[ii][jj][kk] = ii*1000000 + jj*1000 + kk;
          }
        }
      }
    }
    
    
    void out (int *p, int nz, int ny, int nx)
    {
      int (*A)[ny][nx] = (int(*)[ny][nx]) p;
      cout << A[11][22][33] << endl;
    }
    
    
    int main (void)
    {
      int NX = 97;
      int NY = 92;
      int NZ = 20;
      int *space = new int [NZ * NY * NX];
    
      bar (space, NZ, NY, NX);
      out (space, NZ, NY, NX);
      return 0;
    }
    

    Running this produces the output "11022033"

    The declaration of the "A" alias is a little weird looking, but it allows you to directly and simply use the desired multi-dimensional array syntax

    0 讨论(0)
  • 2020-12-02 10:54
    const int someheight = 3;
    const int somewidth = 5;
    
    int (*array)[somewidth] = new int[someheight][somewidth];
    
    0 讨论(0)
  • 2020-12-02 10:55

    I think this will do

    int r, c ;
    std::cin>>r>>c ;
    int *array = new int[r*c] ; 
    

    You can input the values by doing something like this

    for (int i = 0 ; i < r ; i++){
        for (int j = 0 ; j < c ; j++){
            std::cin>>array[i *c + j] ; 
        }
    }
    
    0 讨论(0)
  • 2020-12-02 10:58

    I suggest using a far simpler method than an array of arrays:

    #define WIDTH 3
    #define HEIGHT 4
    
    int* array = new int[WIDTH*HEIGHT];
    int x=1, y=2, cell;
    cell = array[x+WIDTH*y];
    

    I think this is a better approach than an array of an array, as there is far less allocation. You could even write a helper macro:

    #define INDEX(x,y) ((x)+(WIDTH*(y)))
    
    int cell = array[INDEX(2,3)];
    
    0 讨论(0)
  • 2020-12-02 10:59

    I just found this ancient answer still gets read, which is a shame since it's wrong. Look at the answer below with all the votes instead.


    Read up on pointer syntax, you need an array of arrays. Which is the same thing as a pointer to a pointer.

    int width = 5;
    int height = 5;
    int** arr = new int*[width];
    for(int i = 0; i < width; ++i)
       arr[i] = new int[height];
    
    0 讨论(0)
提交回复
热议问题