Jump to content
  • Advertisement

Archived

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

thorpe

Dynamic memory allocation question

This topic is 5896 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!