Thread: Delete / Clear

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    Delete / Clear

    Hello guys here's the problem. I want to delete the objects within a vector, although I'm not sure whether I should clear the vector afterwards. Is it really necessary?

    Code:
    for (i = 0; i < allSales.size(); i++)
                delete allSales[i];
    
    allSales.clear(); //Is this step necessary?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is allSales storing pointers that you allocated with new? If not, then delete itself is incorrect.

    The purpose of clear is to remove the objects from the vector, so if that's what you want done, then clear is the way to go.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Yes, it is a vector of pointers.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Khabz View Post
    Yes, it is a vector of pointers.
    In that case, the delete calls will release the objects being pointed to, and clear will then remove the pointers from the vector (I would assume you don't want them hanging around still, cluttering up your vector now that they don't point anywhere).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If the vector is going to be destroyed (e.g., by going out of scope) soon afterwards, then you don't need the call to clear.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Call clear() if there is the possibility that something else could or might see the vector full of pointers and mistakenly think they are valid.
    Even then, it might be less confusing to someone debugging the code later if a tidy call to clear() ensured that the pointers were gone.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector::clear() won't clear
    By Ducky in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2013, 04:23 AM
  2. Concerning delete/delete[] at program exit
    By laserlight in forum C++ Programming
    Replies: 58
    Last Post: 01-09-2008, 01:40 PM
  3. Replies: 17
    Last Post: 11-16-2006, 09:06 PM
  4. Using delete to clear a STL vector
    By starkhorn in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2005, 02:59 AM
  5. using delete to delete an array
    By iain in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2002, 03:53 PM