Thread: Multi-Demensional Arrays on Heap

  1. #1
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140

    Lightbulb Multi-Demensional Arrays on Heap

    are multi-demensional arrays on the heap impossible?
    try this out on your compiler. get an error dev 4.9.4.1

    Code:
    int * pInt = new int[2][2];
    delete pInt;
    my error says Cannot assign to int * from int[*][2] or something very similar
    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not impossible
    Code:
    const int max_cols=20;
    
    // use this when rows and columns are truly dynamic
    // like int arr[x][y]
    void f1 ( int num_rows, int num_cols ) {
        int **p;
        int i;
    
        p = new int*[num_rows];
        for ( i = 0 ; i < num_rows; i++ ) p[i] = new int[num_cols];
    
        // use it
    
        for ( i = 0 ; i < num_rows ; i++ ) delete p[i];
        delete p;
    }
    
    // use this if colums is a compile time constant
    // like int arr[x][10];
    void f2 ( int num_rows ) {
        int (*p)[max_cols];
    
        p = new int[num_rows][max_cols];
    
        // use it
    
        delete p;
    }
    In both these functions, you can use the allocated array as p[row][col]

  3. #3
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    mm that looks very confusing. is there a simpler way? why can't i just type
    Code:
    int * pInt = new int[2][2];
    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.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is there a simpler way?
    You mean like my f2 example

    int * pInt = new int[2][2];
    would be
    int (*pInt)[2] = new int[2][2];

    > why can't i just type
    Because this is for a 1D array of int
    int * pInt = new int[2];
    You can't make it into a 2D array simply by changing the new operator.

    > that looks very confusing
    Then I suggest careful study here
    Pointer declarations, especially the more complex ones are one of the tricker aspects of C and C++ programming.

  5. #5
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    I know the exact size of the array in the program (its going to be a 20x20 array of ROOMS for a text rpg, but i used ints for simplicity), so can I do this?..
    Code:
    ROOM * create()    // so i don't need this parameter?
    {
        ROOM * pRoom[20];      //an array of pointers?
    
        pRoom = new ROOM[20][20];  // replace with numbers?
    
        // can i use pRoom as it were a regular pointer to an array now?
        // such as pRoom[2][3]->method();
    
        return pRoom;
    }
    the link your gave is for C, and i dont recognize most of the printfs and all that stuff..

    could you explain why i needed to declare pRoom as an array of 20 ROOMS?
    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.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <iostream>
    
    
    class Room{ //Your class.....whatever.....
    public:
    	int member;
    };
    
    
    int main(void)
    {
    	Room** room; //pointer to a pointer to Room
    	int x,y; 
    
    	/*Set up your array*/
    
    	room = new Room*[20];//Set first dimensions
    
    	for(x = 0;x < 20;x++)
    		room[x] = new Room[20];//Set second dimensions
    
    	/*Do whatever with it*/
    
    	for(x = 0;x < 20;x++)
    		for(y = 0;y < 20;y++)
    			room[x][y].member = x+y; //set values...blah blah
    
    	for(x = 0;x < 20;x++)
    		for(y = 0;y < 20;y++)
    			std::cout << room[x][y].member << " ";//Show results
    
    	/* Dont forget to clean up!*/
    
    	for(x = 0;x < 20;x++)
    		delete [] room[x];
    
    	delete [] room;
    
      return 0;
    }
    Last edited by Fordy; 08-11-2002 at 03:05 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I know the exact size of the array in the program
    So why are you even trying to allocate it when you can just say

    ROOM rooms[20][20];

  8. #8
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    that would declare it either globally or on the stack - which i don't want. i'd like it on the heap.
    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. #9
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Originally posted by Fordy
    Code:
    #include <iostream>
    
    
    class Room{ //Your class.....whatever.....
    public:
    	int member;
    };
    
    
    int main(void)
    {
    	Room** room; //pointer to a pointer to Room
    	int x,y; 
    
    	/*Set up your array*/
    
    	room = new Room*[20];//Set first dimensions
    
    	for(x = 0;x < 20;x++)
    		room[x] = new Room[20];//Set second dimensions
    
    	/*Do whatever with it*/
    
    	for(x = 0;x < 20;x++)
    		for(y = 0;y < 20;y++)
    			room[x][y].member = x+y; //set values...blah blah
    
    	for(x = 0;x < 20;x++)
    		for(y = 0;y < 20;y++)
    			std::cout << room[x][y].member << " ";//Show results
    
    	/* Dont forget to clean up!*/
    
    	for(x = 0;x < 20;x++)
    		delete [] room[x];
    
    	delete [] room;
    
      return 0;
    }
    i was wondering if you could explain a bit more of how that code works fordy, i'm reluctant to use code that i don't understand
    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.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Then

    ROOM (*rooms)[20] = new ROOM[20][20];

    Is all you need to get you
    rooms[0][0] to rooms[19][19]

  11. #11
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    salem, are you saying i can skip that other code and just use yours & it will work?
    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.

  12. #12
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    From Fordy's code above:

    Code:
    
    /*Set up your array*/
    room = new Room*[20];//Set first dimensions
    
    This is to allocate space for 20 Room pointers. (20 Room*'s)

    Code:
    
    for(x = 0;x < 20;x++)
        room[x] = new Room[20];   //Set second dimensions
    
    Creating 20 Room objects on the heap, using the 20 pointers allocated initially.

    Code:
    
    /* Dont forget to clean up!*/
    for(x = 0;x < 20;x++)
         delete [] room[x];
    
    delete [] room;
    
    First delete each object, then delete the initial pointer.

  13. #13
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    somehow that makes no sense to me.
    room holds the address to the first of 20 pointers to ROOMs, which is stored on the heap

    then each of those 20 pointers is set up to hold the address of a particular ROOM which is created on the heap again (so far, only 20 ROOMS have been made, not 400)

    then you delete all the pointers.

    it seems this only declares a single-dimensioned array, not a 2-D array like i'm trying to do am i crazy?
    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.

  14. #14
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >>it seems this only declares a single-dimensioned array, not a 2-D array like i'm trying to do am i crazy?

    You're right there!

    Do you want a 2D array of rooms on the heap?
    If you do, then you'll need to do something like this:

    ROOM **rooms[2];

    Then treat each element as in the original code.

  15. #15
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    ROOM **rooms[2];
    What does that do? Not sure I understand how/why pointers to pointers are used.

    What original code?? Everyone has a different idea on how to do it and im not sure how each one works, so im still looking for how to do it.
    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.

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, 04:49 PM
  2. copying strings to heap character arrays
    By SkyRaign in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2006, 02: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, 05:03 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM