Multi-Demensional Arrays on Heap

This is a discussion on Multi-Demensional Arrays on Heap within the C++ Programming forums, part of the General Programming Boards category; > salem, are you saying i can skip that other code and just use yours & it will work? Yes, ...

  1. #16
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > 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. #17
    Unregistered
    Guest
    >You're right there!

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

  3. #18
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> 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. #19
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    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
    Last edited by GrNxxDaY; 08-13-2002 at 12:28 PM.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  5. #20
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >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. #21
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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.
    Think about it.

  7. #22
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >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. #23
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    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
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  9. #24
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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. #25
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >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. #26
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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.

  12. #27
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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. #28
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    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;

    I count 400....how about you?

    20 Pointers to arrays of 20 Room objects

  14. #29
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >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.

  15. #30
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Doh, my bad!

    I didn't see this : new Room[20];

    I assumed it was : new Room;

    I'm sorry Enmeduranki, but i was following my code instead of Fordy's.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  2. copying strings to heap character arrays
    By SkyRaign in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2006, 01:08 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 04:03 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM

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