Archived

This topic is now archived and is closed to further replies.

thorpe

Dynamic memory allocation question

Recommended Posts

Hello! I need some help with my dynamic memory allocation. This is how I allocate memory for my twodimensional matrix;
  
TheQuadrants = new Actor**[WorldSize*11*WorldSize*11];

for(int i=0;i<WorldSize*11;i++){
	for(int z=0;z<WorldSize*11;z++){
		TheQuadrants[i*WorldSize*11 + z]=0;
	}
}
  
That code runs smoothly. The following line runs ok unless I try to read temp later in my program;
  
Actor * temp = TheGame->TheQuadrants[GetXs()][GetYs()];
  
If I read the temp pointer my program crashes when the ABOVE line is run, not when I read temp. TheQuadrants is defined as;
  
Actor *** TheQuadrants;
  
Any help would be appreciated... Johan Torp - http://www.destruction.nu

Share this post


Link to post
Share on other sites
Hi

i guess the problem is that you don''t allocate the memory for the quadrants the right way.
TheQuadrants = new Actor**[WorldSize*11*WorldSize*11];
allocates just one single array, but then you try to access it as a 2-dimensional one:
Actor * temp = TheGame->TheQuadrants[GetXs()][GetYs()];
which causes the crash.
To allocate a 2-dimensional array you''d have to do it like this:

TheQuadrants = new Actor**[WorldSize*11];
for ( int i = 0; i < WorldSize*11; i++)
TheQuadrants = new Actor*[WorldSize*11]

this code allocates a 2-dimensional array of Actor*..i guess this is what you want to do.
Now it should be possible to access TheQuadrants[GetXs()][GetYs()];

Hope that helps



------------
Runicsoft

Share this post


Link to post
Share on other sites