STL: erase - do I need to delete?

This is a discussion on STL: erase - do I need to delete? within the C++ Programming forums, part of the General Programming Boards category; If i have a list of pointers, and I use the erase function, do I need to delete the object ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    36

    STL: erase - do I need to delete?

    If i have a list of pointers, and I use the erase function, do I need to delete the object if it was allocated with new? Or is this taken care of for me?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    No, all allocated memory with new must have a matching delete.
    The STL won't delete the memory for you, only toss aside the pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Or use a list of some smart pointer type instead of a plain ol' pointer, then it would be taken care of.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Yes, RAII classes are recommended. You can have a look at boost's shared_ptr.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    You can have a look at boost's shared_ptr.
    It may be available as std::tr1::shared_ptr too.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Or to avoid this overhead, you can use Boost's pointer_containers.
    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

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    And of course make sure that you really do need/want to store pointers in your container. There are times when it's not necessary and storing objects is preferred.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL List Erase
    By shamgar00 in forum C++ Programming
    Replies: 7
    Last Post: 10-22-2007, 02:58 PM
  2. Using delete to clear a STL vector
    By starkhorn in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2005, 02:59 AM
  3. How do you delete an STL map<long,class *>
    By ouagadougou in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2003, 05:21 AM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21