Archived

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

TykeiL

dynamic memory deletion

Recommended Posts

TykeiL    122
my question today is about dynamic memory allocation, i have been reading this book which describes a little about it but since im using pointers to pointers to pionters and need to delete the memory i dont know whats going on with it. heres my code,
char Buffer[MAX_PATH]
float ***geometry;
float ***Ngeometry;
float ***Cgeometry;
float ***Tgeometry;
int numfaces;

void LoadModel()
{	

	fstream scenefile;
	scenefile.open(Scene,ios::in | ios::out);

	scenefile >> Buffer;
	numfaces = atoi(Buffer);
	geometry = new GLfloat**[numfaces];
	Ngeometry = new GLfloat**[numfaces];
	Cgeometry = new GLfloat**[numfaces];
	Tgeometry = new GLfloat**[numfaces];
	
	for(int x = 0; x < numfaces; x++)
	{
		geometry[x] = new GLfloat*[3];
		Cgeometry[x] = new GLfloat*[3];		
		Ngeometry[x] = new GLfloat*[3];
		Tgeometry[x] = new GLfloat*[3];
		
		for(int y = 0; y < 3; y++)
		{
			geometry[x][y] = new GLfloat[3];
			Ngeometry[x][y] = new GLfloat[3];			
			Cgeometry[x][y] = new GLfloat[3];
			Tgeometry[x][y] = new GLfloat[3];			

			scenefile >> Buffer;
			geometry[x][y][0] = (GLfloat)atof(Buffer);
			scenefile >> Buffer;
			geometry[x][y][1] = (GLfloat)atof(Buffer);
			scenefile >> Buffer;
			geometry[x][y][2] = (GLfloat)atof(Buffer);

			scenefile >> Buffer;
			Ngeometry[x][y][0] = (GLfloat)atof(Buffer);
			scenefile >> Buffer;
			Ngeometry[x][y][1] = (GLfloat)atof(Buffer);
			scenefile >> Buffer;
			Ngeometry[x][y][2] = (GLfloat)atof(Buffer);

			scenefile >> Buffer;
			Cgeometry[x][y][0] = (GLfloat)atof(Buffer);
			scenefile >> Buffer;
			Cgeometry[x][y][1] = (GLfloat)atof(Buffer);
			scenefile >> Buffer;
			Cgeometry[x][y][2] = (GLfloat)atof(Buffer);
			
			scenefile >> Buffer;
			Tgeometry[x][y][0] = (GLfloat)atof(Buffer);
			scenefile >> Buffer;
			Tgeometry[x][y][1] = (GLfloat)atof(Buffer);
			scenefile >> Buffer;
			Tgeometry[x][y][2] = (GLfloat)atof(Buffer);
		}
	}
}

void UnLoadModel()
{
// this is where i need my deletion code but i have no clue

// ive tried stuff like

//delete [] **geometry; in nested for statements to delete each

//tier of memory allocation but i get errors

}
thanks if anyone can help me with this, the book i have doesnt go into much detail about deleting memory from pointers to pointers !!My bRaiN is FiLLEd With HappY Juice!!

Share this post


Link to post
Share on other sites
JohnyB    122
I guess you can do something like this for every object

void UnLoadModel()
{
int i, j;
for( i = 0;i < numfaces; i++ )
{
for( j = 0;j < 3;j++ )
{
delete [] geometry[i][j];
}
delete [] geometry[i];
}
delete [] geometry;
geometry=NULL;
}


I would just stay away of doing it this way, maybe is just me but is a lot easier to have everything in an array of structures. It's a lot more readable as well...


[edited by - JohnyB on January 15, 2004 5:45:27 AM]

Share this post


Link to post
Share on other sites