# Thread: Multi-Demensional Arrays on Heap

1. > salem, are you saying i can skip that other code and just use yours & it will work?
Yes, try it now and see for yourself

2. >You're right there!

No, he/she's crazy. 20 pointers each pointing to 20 rooms equals 400 rooms.

3. >> No, he/she's crazy. 20 pointers each pointing to 20 rooms equals 400 rooms.

NO. There are only 20 rooms.

Do some research.

4. SALEM SALEM SALEM, u might be a genious, i did
Code:
`ROOM (*room)[20] = new ROOM[20][20];`
and it compiled without problem. i haven' t tested out the room[3][7].whatever() thing yet, though.

i have 2 question for you
1)what does the Lvalue on the assignment statment mean? the [20] slightly confuses me.

2)how can i return the room pointer back to main? what return type do i use? it seems ROOM* doesnt work

5. >NO. There are only 20 rooms.

Ok, lets go through it -

room = new Room*[20];//Set first dimensions

Creates an array of 20 pointers to Rooms. Agreed?

>for(x = 0;x < 20;x++)
room[x] = new Room[20]; //Set second dimensions<

Allocates 20 Rooms per iteration of the loop and assigns in each iteration the memory to one of the previously allocated 20 pointers to room.

My maths may be a bit flaky but I'm sure 20x20=400.

6. I refuse to get all fussy here, because this aint helping GrNxxDaY.

Here's one way to explain it:

Q. What do you need to create an object on the heap?
A. A pointer.

In the previous code we had 20 pointers, so we created 20 Room objects on the heap.

There aren't 400 rooms, only 20.

7. >because this aint helping GrNxxDaY

You're spot on there, guvnor. I think Salems done that anyway.

What does this code do -

int* ptr = new int[20];

?

How many ints are pointed to by ptr? 20, do you agree? Now suppose you have an array of 20 ptrs (instead of the solitary one we have here). You can perform the above allocation for each of these 20 ptrs. 20x20 !=20. Phew.

8. To me it doesn't matter whose right - try not to flood the thread with too much argument though.

What i'm trying to get at is this:
1) in a function, declare room as a pointer to 2-D array of ROOMS on the heap
2) return room from the function where it is defined to the calling function (main)
3) once returned, room should be able to do this.. room[3][15].method()

if that isn't possible, how can i obtain the same results? if you know of a way to do this - could you write the code down and explain how it works? i know some of you have written the code - i'm reluctant to use it because i dont know how it works so i can't modify it if i ever wanted to

9. You can perform the above allocation for each of these 20 ptrs. 20x20 !=20. Phew.

NO, you can't. You're not getting it.

This declaration:

int *ptr = new int[20];

creates an array of 20 ints, the same way that

char *ptr = new char[50];

creates an array of 50 chars.

Now let's say we had this:

char **list = new char*[20];

This allocates memory for 20 char*'s.

for(int i = 0;i<20;i++)
list[i] = new char[50];

create 50 char arrays.

NOW, there aren't 100 char arrays, but only 20, each having 50 elements. This is a 2D array, but there are only 20 arrays in list.

The same way that in the previous example, there were only 20 Room objects, although it was a 2D array.

10. >there aren't 100 char arrays, but only 20, each having 50 elements

Assuming your code was correct this would be a two dimensional array of 1000 chars. In the same way the Fordy's original example was a two dimensional array of 400 rooms.

I suggest you both go and read the link posted by Salem near the start of the thread.

11. Here GrNxxDaY:

Code:
```#include <iostream.h>

public:
int member;
};

#define FIRST	2
#define SECOND	20

Room*** function()
{
Room*** room;
room = new Room**[FIRST];

for(int x = 0;x < FIRST;x++)
room[x] = new Room*[SECOND];

for(int i=0;i<FIRST;i++)
for(x = 0;x < SECOND;x++)
room[i][x] = new Room;

return room;
}```
As for this :

>>Assuming your code was correct this would be a two dimensional array of 1000 chars.

So how many arrays does list have? 20
Each having 50 elements.

>>I suggest you both go and read the link posted by Salem near the start of the thread.

>>In the same way the Fordy's original example was a two dimensional array of 400 rooms.

I think you should realize that i am right. Have you tried it?
You'll notice that you'll only have 20 Room objects.

12. flip, i forgot main()

Code:
```int main(void)
{
Room ***room;  //pointer to a 2D array

room = function();
//now you have 40 rooms to play with

room[0][1]->member = 5;

cout << room[0][1]->member;

/* Dont forget to clean up!*/
for(int i=0; i< FIRST;i++)
for(int x = 0;x < SECOND;x++)
delete [] room[i][x];

delete [] room[0];
delete [] room[1];

delete room;

return 0;
}```

13. Originally posted by The Dog

>>In the same way the Fordy's original example was a two dimensional array of 400 rooms.

I think you should realize that i am right. Have you tried it?
You'll notice that you'll only have 20 Room objects.

Hmm....try change the part where I fill the arrays to

Code:
```int z = 0;
for(x = 0;x < 20;x++)
for(y = 0;y < 20;y++)
room[x][y].member = ++z;```

20 Pointers to arrays of 20 Room objects

14. >Have you tried it?

I've written code that relies on it.

I dunno, maybe you're confused by for loops. How many ints have we got here -

//dynamically allocated an array of pointers
//this isn't necessary if one of your dimensions
//is a compile time constant
int** array = new int*[20];

//allocate an array of 20
//integers (yes, 20 and they're integers you know)
//to each of the previously allocated pointers

array[0]=new int[20];
array[1]=new int[20];
array[2]=new int[20];
array[3]=new int[20];
array[4]=new int[20];
array[5]=new int[20];
array[6]=new int[20];
array[7]=new int[20];
array[8]=new int[20];
array[9]=new int[20];
array[10]=new int[20];
array[11]=new int[20];
array[12]=new int[20];
array[13]=new int[20];
array[14]=new int[20];
array[15]=new int[20];
array[16]=new int[20];
array[17]=new int[20];
array[18]=new int[20];
array[19]=new int[20];

There's quite a few 20s there.