Thread: Call to new to create objects that go into a vector

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What is the best way to release memory that was used in making an object with new?
    There is only one way: delete. It doesn't matter whether a pointer to an object created with new is stored in a vector, in an array, or in a single variable, you need to use delete. You might want to take this approach:

    1) Define a destructor for your class. Have your destructor output the message: "destructor called".

    2) Practice deleting a pointer that is stored in a single variable.

    3) Practice deleting an array of pointers. Look at the number of messages you get from your destructor, and check if you get the right number of messages.

    4) Try using delete on the pointers stored in your vector.
    Last edited by 7stud; 02-11-2006 at 03:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Inline asm
    By brietje698 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2007, 02:54 PM
  3. Custom Vector template: Want to create erase/clear function
    By Bird Killer in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:37 AM
  4. API Call to create folder
    By cboard_member in forum Windows Programming
    Replies: 2
    Last Post: 12-01-2005, 12:08 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM