# Thread: Pointer to a dynamic multi-dimensional array

1. ## 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. 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);
}```

3. 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.

4. Small correction: The fist line should allocate pointers
Code:
`int **array =new int*[row];`

5. ## 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. 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]```

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

8. Originally posted by golfinguy4

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?

9. 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.

10. ## 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. >> 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'.