Advertisement

3d Dynamic arrays

Started by August 06, 2000 12:17 AM
3 comments, last by RiAL 24 years, 4 months ago
Hello I created a 3d dynamic array like the one shown in one of the GameDev tutorials, but now I''m having a lot of trouble deleting it. When I go into debug mode and exit I have *alot* of memory leaks. Any ideas would be appreciated!
Could you post some code? Just the bits where you ''new'' the array and ''delete'' it will help.

Just a thought, are you using ''delete [] array'' and not ''delete array''?

Alan
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
Advertisement
I created the array like so:
    	Map = (CMapInfo***) malloc(Layers * sizeof( CMapInfo *) );	for(int x=0; x<Layers; x++)	{		Map[x] = (CMapInfo**)malloc(MapWidth * sizeof(CMapInfo));		for(int y=0; y<MapWidth; y++)		{			Map[x][y] = (CMapInfo*)malloc(MapHeight * sizeof(CMapInfo));		}	}	// Initialize values	for(int layer=0; layer<Layers; layer++)	{		for(int xvalue=0; xvalue<MapWidth; xvalue++)		{			for(int yvalue=0; yvalue<MapHeight; yvalue++)			{				Map[layer][xvalue][yvalue].TileID = 0;				Map[layer][xvalue][yvalue].Walkable = 0;				}		}	}    

How would I go about deleting it now? I'm not exacly sure if this is the right way of creating the array as well. However I can access the values.

I don't have the delete[] code any more.

Edited by - Rial on August 7, 2000 2:10:25 AM
Oh, that''s some pretty nasty stuff. You''re going to have to undo each call to malloc in the reverse order to which the memory was reserved:

    int x, y;for (x = 0; x < Layers; x++)  for (y = 0; y < MapWidth; y++)    free(Map[x][y]);for (x = 0; x < Layers; x++)  free(Map[x]);free(Map);    


Enjoy!

Osc.
Thanks! That worked . By the way do you have any suggestions on doing it a different way?

This topic is closed to new replies.

Advertisement