Thread: Dynamic memory

  1. #1

    Dynamic memory

    I'm having problems with dynamic memory. I am trying to dynamically allocate a structure with a 3D array. Here is the parts of my code I need help with:

    Code:
    class map
    {
     private:
     struct new_tile
     {
      ...
     }*tiles;
    ...
    }
    
    ...
    
    map::map(char *whichmap)
    {
     ...
     tiles = new new_tile[tilesacross][tilesdown][tilelayers];
     ...
    }
    It says this as the error message

    54 maps.h
    assignment to `map::new_tile *' from `map::new_tile (*)[((tilesdown - 1) + 1)][((tilelayers - 1) + 1)]'

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I think it has to be something like:
    Code:
    tiles = new new_tile[tilesacross*tilesdown*tilelayers];
    You then have to come up with some method of treating this single chunk of memory like a three dimension array of new_tile objects.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    you can only do 1D arrays with the new keyword?

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    Yes, in one statement. It is possible to manually allocate multi-dimensional arrays but it gets pretty messy (not to mention the potential for poor performance) so it's probably just as easy to allocate on block and calculate your own offsets. Here's one way of allocating a three dimensional array -

    Code:
    int main()
    {
    	int a =10;
    	int b =5;
    	int c =7;
    
    	int*** ptr = new int**[a];
    
    	for(int i=0;i<a;++i)
    	{
    		ptr[i] = new int*[b];
    		
    		for(int j=0;j<b;++j)
    		{
    			ptr[i][j] = new int[c];
    		}
    	}
    
    	ptr[0][1][2] = 12313;
    
    
      return 0;
    }
    To de-allocate you, do it the other way round (start by de-allocating the third dimension).

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > tiles = new new_tile[tilesacross][tilesdown][tilelayers];
    Are any of these parameters compile time constants?

    Preferably two of them should be constants, but one would be better than none

    Otherwise, it's as Enmeduranki wrote

  6. #6
    they are not compiled, they are variables. I'm making this so the map files it reads can have different width's height's and layers for a bunch of customization.

    what the heck is int*** ptr;? Does that make like three pointers or something?

  7. #7
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    It's a pointer to a pointer to a pointer.
    Remember a int * is essentially equivalent to int [x].

    A two dimensional array int [x][y] can be refered to as int **

    and the three dimensional array int [x][y][z] as int ***
    Last edited by foniks munkee; 07-10-2002 at 06:17 PM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  8. #8
    I got it to work

    And if I just use delete [] tiles, will it free up the memory correctly? It compiles and runs correctly.

  9. #9
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    How did you allocate the memory?
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  10. #10
    very carefully

  11. #11
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Yes, well...

    I guess what I am saying is it depends on how you allocated the memory as to whether that particular method is correct. Quite often the incorrect method will compile fine and run fine - but in fact be very very wrong.

    So, if you can show the code that you used to allocate the memory - we can tell as to whether delete [] tiles is correct.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 09:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM