Pointer to a dynamic multi-dimensional array

This is a discussion on Pointer to a dynamic multi-dimensional array within the C++ Programming forums, part of the General Programming Boards category; Could someone please tell me why the following code does not work for a dynamic multidimensional array. Thank you. Code: ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Pointer to a dynamic multi-dimensional array

    Could someone please tell me why the following code does not work for a dynamic multidimensional array. Thank you.
    Code:
    HashTable::HashTable()
      // default constructor
    {
      // create a dynamically-allocated table of type String
      // with row equal to default size and column equal to 2
      int **p; // declare a pointer of type String
    
      p = new int[TABLE_SIZE][2];
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    First you have to allocate for the **, then loop thru and allocate each column:

    String ** p;

    p = new String * [TABLE_SIZE];

    for(int i = 0; i < TABLE_SIZE; i++)
    p[i] = new String[2];


    A safer way would be to do the work inside a function:


    Code:
    template <class type>
    type ** alloc_multidim(int rows, int columns)
    {
     type ** memory = new type * [rows];
     for(int index = 0; index < rows; ++index)
      memory[index] = new type[columns];
     return memory; 
    }
    
    template <class type>
    void dealloc_multidim(type ** memory, int rows)
    {
     for(int index = 0; index < rows; ++index)
      delete[] memory[index];
     delete [] memory; 
    }


    Code:
    int main()
    {
     String * p = alloc_multidim<String>(TABLE_SIZE, 2);
     dealloc_multidim(p, TABLE_SIZE);
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    another way to do it is like this

    Code:
    int **array =new int[row]; 
     
    int *p = new int[row * col]; 
    int i; 
     
    for(i = 0; i < row; i++) 
    { 
        array[i ] = p + (col * i); 
    }
    good thing about using contiguous dynamic memory is that you only have to delete two arrays after you've finished.
    Last edited by Kyro; 09-20-2003 at 02:06 AM.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Small correction: The fist line should allocate pointers
    Code:
    int **array =new int*[row];
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Conceptually

    Alright guys, thank you for your posts. It seems to work syntactically but I do not understand why you would put two *'s next to a pointer for a 2-d array? Could someone explain this a little bit more clearly or if they have any links that I might visit that would help me out a lot. Thanks a lot.

  6. #6
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    Okay, you started a thread in the middle of a conversation, but I know what you're talking about. Says something about me, doesn't it?

    Anyway, a pointer to a pointer is used because you need an array of arrays. To create a one dimensional dynamic array, you use a pointer:
    Code:
    int *ia = new int[10];
    Simple, but say you want an array of ia's. Well, you do the same thing except make an array of pointers to int:
    Code:
    int **ia = new int*[10];
    Then make a dynamic array for each pointer in that array:
    Code:
    for (int i = 0; i < 10; i++)
      ia[i] = new int[10];
    The end result is a simulation of a two dimensional array by having an array of arrays:
    Code:
    [0]->[0][1][2][3][4][5][6][7][8][9]
    [1]->[0][1][2][3][4][5][6][7][8][9]
    [2]->[0][1][2][3][4][5][6][7][8][9]
    [3]->[0][1][2][3][4][5][6][7][8][9]
    [4]->[0][1][2][3][4][5][6][7][8][9]
    [5]->[0][1][2][3][4][5][6][7][8][9]
    [6]->[0][1][2][3][4][5][6][7][8][9]
    [7]->[0][1][2][3][4][5][6][7][8][9]
    [8]->[0][1][2][3][4][5][6][7][8][9]
    [9]->[0][1][2][3][4][5][6][7][8][9]
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  7. #7
    ˇAmo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    How about being safe and using a 2d vector instead.

    std::vector < std::vector <int> > foo

  8. #8
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Originally posted by golfinguy4
    How about being safe and using a 2d vector instead.

    std::vector < std::vector <int> > foo
    For critical apps it can be slower. Besides it´s better to use the old way if know the exact size, isn´t?
    Nothing more to tell about me...
    Happy day =)

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by gustavosserra
    For critical apps it can be slower. Besides it's better to use the old way if know the exact size, isn´t?
    Actually, with a good optimizing compiler, it's not at all slower. The access function (operator[]) will be inlined, and the access time is exactly the same.

    And, I don't think there is EVER a case (with the exception of times you need arrays of bool) when it is better to use a dynamic array in place of a vector.

    This code:

    Code:
    int nRow = 4, nCol = 6;
    typedef std::vector<int> iRow;
    typedef std::vector<iRow> iArray;
    
    iArray myArray(nRow,iRow(nCol));
    looks a lot cleaner than:

    Code:
    int nRow = 4, nCol = 6;
    
    int ** myArray = new int*[nRow];
    for (int i = 0; i < nRow; i++) myArray[i] = new int[nCol];
    
    /* ... */
    for (int i = 0; i < nRow; i++) delete[] myArray[i];
    delete[] myArray;
    The only reason I would use the built-in arrays for dynamic memory was if I needed to create an array that was contiguous in memory. For example, one way of creating and accessing a display buffer might be:

    Code:
    int * videoBuffer = new int[640*480];
    int ** video = new int *[640];
    for (int i = 0; i < 640; i++)  video[i] = videoBuffer + 480*i;
    
    // Now you could use video[45][64] to access the correct position in videoBuffer
    
    delete[] video;
    delete[] videoBuffer;
    However, unless I really needed to do something where I had to have an array contiguous in memory, I wouldn't bother.
    Last edited by Cat; 09-20-2003 at 05:45 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    On Dynamic Multi-dimensional arrays and hash tables

    Hey all,
    I would like to like to know if there are any links that I might go to get a detailed explanation of dynamic multi-dimensional arrays. It seems like I would have to dynamically re-size my 2D-array later on.
    Another question... and this has been bugging me since I started this assignment... I would like to know if I could use a 2D array to implement a hashtable (in this case, the application is an address book) that takes in a name and an address. Now the only way that I could conceive of the idea of a hash table as a data structure is by having a two-dimensional array, that being n rows and 2 columns, one cell for the name and another for the address. Just wanted to know if I was on the right track.
    Thank you in anticipation.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> It seems like I would have to dynamically re-size my 2D-array later on.

    Just look at the code that has been posted. From that it's easy to see how to resize it. Otherwise, just use a vector - much easier to work with.

    >> Now the only way that I could conceive of the idea of a hash table...

    A hash table is an array of pointers where each element points to one or more data nodes. The hashing algorithm generates the index for each entry. If there are multiple entries at a single index, they just get chained together. As to having *two* pieces of data in a hash table, that just doesn't work that way. You would have to make each data node hold two pieces of data, and choose which will be the 'key'.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic 2 Dimensional Array
    By mrb260478 in forum C Programming
    Replies: 23
    Last Post: 06-21-2008, 07:19 AM
  2. Two dimensional array
    By George2 in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 04:27 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Dynamic pointer array in C
    By MacFromOK in forum Windows Programming
    Replies: 14
    Last Post: 04-09-2005, 06:14 AM
  5. Multi dimensional array
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21