Thread: Using delete to clear a STL vector

  1. #1
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21

    Using delete to clear a STL vector

    Folks,

    I've created 2 classes, called TEST1 and TEST2. In TEST1, I've declared a STL vector of the TEST2 class. Each time my program is run, there is a random number generated which specifies the number of instances of TEST2 that should be contained within the vector. That obviously required a initialisation function which I've written below

    Code:
    vector<TEST2> test2_list;
    
    for (int i = 0; i < random_num; i++)
    	{
    		TEST2 *temp = new TEST2(param1, param2, param3);
    		test2_list.push_back(*temp);
    	}
    This works great. It initialises the vector exactly as I need it.

    My question is now on how do I delete the memory that I used with the new command once the program has stopped running.

    Do I put in the deconstructor of TEST1 a command to clear the vector. Will that delete the memory ?

    Code:
    test2_list.clear();
    Or do I need to put something in the deconstructor of TEST2 like

    Code:
    delete this;
    Unsure on how to delete this memory again once this program is complete. Obviously at the moment, I've got some memory leak so it's certainly a problem.

    Any help, would be much appreciated.

    Cheers
    Starkhorn

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    do you mean that you wanna delete the memory when the vector is destructed?
    if so, don't care about this, the vector will destruct the elements object and delete the memory automatically.
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    for (int i = 0; i < random_num; i++)
    {
    	TEST2 *temp = new TEST2(param1, param2, param3);
    	test2_list.push_back(*temp);
    }
    You're apparently not aware of what this code does. You allocate memory for a TEST2 object and initialize it. Then, you put a copy of that object into the vector. The vector automatically allocates memory to hold it. Then you start the next iteration, thus losing the pointer to the memory you just allocated and creating an irrecoverable memory leak.

    Since the way you have it now is "working", you probably want this:
    Code:
    test2_list.reserve(random_num); // This line makes the loop faster.
    for (int i = 0; i < random_num; i++)
    {
    	test2_list.push_back(TEST2(param1, param2, param3));
    }
    In that case, you don't need to worry about the memory anymore. When the vector is destructed, the associated memory gets freed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector in c++ stl
    By dpp in forum C++ Programming
    Replies: 43
    Last Post: 05-12-2009, 10:25 AM
  2. seg fault in vector delete function
    By tytelizgal in forum C Programming
    Replies: 3
    Last Post: 10-18-2008, 04:45 PM
  3. Replies: 25
    Last Post: 04-29-2008, 06:32 AM
  4. Unresolved external symbol using STL vector reference
    By raziel2k in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2007, 04:28 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM