Results 1 to 2 of 2

Thread: Proper deletion;

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    84

    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];

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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
  •  



Click Here to Expand Forum to Full Width