Multi-Demensional Arrays on Heap

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 08-13-2002
Salem
> 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
• 08-13-2002
Unregistered
>You're right there!

No, he/she's crazy. 20 pointers each pointing to 20 rooms equals 400 rooms.
• 08-13-2002
The Dog
>> No, he/she's crazy. 20 pointers each pointing to 20 rooms equals 400 rooms.

NO. There are only 20 rooms.

Do some research.
• 08-13-2002
GrNxxDaY
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
• 08-13-2002
Enmeduranki
>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.
• 08-13-2002
The Dog
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.
• 08-13-2002
Enmeduranki
>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.
• 08-13-2002
GrNxxDaY
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
• 08-13-2002
The Dog
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.
• 08-13-2002
Enmeduranki
>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.
• 08-13-2002
The Dog
Here GrNxxDaY:

Code:

```#include <iostream.h> class Room{ //Your class.....whatever..... 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.
• 08-13-2002
The Dog
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; }```
• 08-13-2002
Fordy
Quote:

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
• 08-13-2002
Enmeduranki
>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.
• 08-13-2002
The Dog