2d array using pointers

This is a discussion on 2d array using pointers within the C++ Programming forums, part of the General Programming Boards category; The problem is that I need a 2d matrix for a map which is defined in a config file. The ...

  1. #1
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320

    2d array using pointers

    The problem is that I need a 2d matrix for a map which is defined in a config file. The map is not a set height or width so I need to have the ability to either make the map any size between 1x1 to 1000x1000. Is there some way to get around this or do I do what all the other games do and use a few sets of numbers for map sizes like 256x256 128x512 etc?

    Code:
    int **grid;
    .
    .
    .
    load>>x>>y>>input;	//get dimentions of the map
    grid = new int[x][y];
    .
    .
    .
    errors:

    Code:
      error C2540: non-constant expression as array bound
    
      '=' : cannot convert from 'int (*)[1]' to 'int ** '
      Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Correct. Dynamic memory allocation is one solution.

    int **p = new int*[x];

    You need to dynamically allocate memory for each element in the first dimension.

    Kuphryn

  3. #3
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    then what about the y dimention?
    Code:
    int **p = new int*[x];
    //something here for y?

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Use a loop:
    Code:
    int** p = new int*[x];
    for(int i = 0; i != x; ++i)
       p[i] = new int[y];
    Delete[] with a similar loop.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Alright thnx.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Or... you could just use a vector of vectors which allows non-constant values on initialization:
    Code:
    std::vector<std::vector<int> > grid(x, std::vector<int>(y));
    With vector you also don't have to remember to delete the memory when you are done with it.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    And Daved's solution really is the prefered one.

    If you do decide to use standard arrays, I really recommend encapsulating your functionality in a class, so that all initialization and cleanup is automatic.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    O it is in a class was just a little confused about the ** thing this is just for loading anyways so I got my constructor doing grid = NULL since in my desructor I assumed that the best way to destruct this is by the following code:
    Code:
    Map::~Map()
    {	if(tilesetpath)delete tilesetpath;
    	if(grid)
    	{
    		for(int i = 0;i < y;i++)
    		{
    			delete grid[i];
    		}
    		delete grid*[x];
    	}
     	if(npclist)delete npclist;
    }

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    There is no need to check something against null before deleting it. You also need to use delete [] to delete arrays. The proper syntax of the destructor would be:
    Code:
    delete tilesetpath;
    if(grid)
    {
    	for(int i = 0;i < x;i++)
    	{
    		delete [] grid[i];
    	}
    	delete [] grid;
    }
    delete npclist;
    Also not that you should loop until x, not y, since x is the number of rows.

  10. #10
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    It's microsoft vs.net it gets mad if I try to delete a NULL array so I gotta check it for whatever reason it's gay and they should change it even though the standard says you don't need to....

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    That rather surprises me. Is it perhaps a odd setting somewhere in VS.NET... I'd expect it to comply with the standard in this regard.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I didn't mean the two dimensional array, I meant the other things.

  13. #13
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Hey it's microsoft why do they need to adhear to a standard that wasn't written 100% by them? But anyways I check it just to make sure.

  14. #14
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Quote Originally Posted by Daved
    I didn't mean the two dimensional array, I meant the other things.
    so
    Code:
    Map::~Map()
    {	if(tilesetpath)delete tilesetpath[];
    	if(grid)
    	{
    		for(int i = 0;i < y;i++)
    		{
    			delete grid[i];
    		}
    		delete grid*[x];
    	}
     	if(npclist)delete npclist[];
    }
    ??

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Use Daved's code for the destructor.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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