Jump to content
  • Advertisement


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


Dynamic memory allocation question

This topic is 5985 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

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


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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!