|
-
May 18th, 2001, 05:21 PM
#1
Thread Starter
Lively Member
Proper deletion;
I have a class CPrimitive that holds a pointer to an UNLIT_VERTEX user defined structure. In my constructor I assign the UNLIT_VERTEX pointer to an array like this;
Unlit_Vertices = new UNLIT_VERTEX[size];
in the destructor which method is better(proper) for the deletion of Unlit_Vertices.
Code:
delete Unlit_Vertices;
or
for(int i=0; i<size; i++)
delete Unlit_Vertices[i];
-
May 18th, 2001, 08:26 PM
#2
Frenzied Member
You should free allcated memory in exatly the opposite way to the way you allocated it. If you have made an array of pointers and allocated each element individually then you would use the for loop method, but you didn't, you allocated the entire array at once.
deallocating arrays is a special case using the new/delete operators, you have to use delete [] instead of delete, otherwise things can go wrong. I'm not sure what happens if you don't do it properly, you probably just get a memory leak.
So, the correct way to deallocate the memory in your exampl is this:
Code:
delete [] Unlit_Vertices; //deallocate the array
Harry.
"From one thing, know ten thousand things."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|