Results 1 to 6 of 6

Thread: Vectors & auto-"delete"

  1. #1
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Vectors & auto-"delete"

    I'm making a program in which I have several vectors which contain pointers to object & I need to move those pointers from one vector to another.

    Now, let's make an example as simple as possible and say I have 2 vectors: atHome & notHome. Both contain pointers to CDs (so we have 2 std::vector<*CD>). Now, I move a CD from my home to a friend's. Thus, the pointer needs to be removed from atHome & added to notHome. The adding is simple enough, I just do a push_back(). However, I can't find a way to remove the pointer from the atHome vector. Both pop_back() & erase() call the destructor of the object they're removing, so I obviously can't use that or I would lose the CD object & the notHome vector would contain an invalid pointer (same goes for clear() if I wanted to remove all the pointers from the vector).

    I'm sure there's an easy way to solve this, but I can't come up with one. I know in this example you could just add a location field to the CD class, but in the program I'm making such a thing is not possible and also that would cause an incredible amount of overhead, since each time you'd want to find every CD at home, you'd need to iterate through the complete vector.

    And so I continue on my quest to fill this forum with mostly newbie questions .

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Vectors & auto-"delete"

    std::vector::pop_back() does not delete anything.
    Last edited by jpn; 11th January 2008 at 09:43. Reason: Disabled smilies
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    Darhuuk (11th January 2008)

  4. #3
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Vectors & auto-"delete"

    Hm, the C++ Reference says it does: http://www.cplusplus.com/reference/s.../pop_back.html.

    void pop_back ( );

    Delete last element

    Removes the last element in the vector, effectively reducing the vector size by one and invalidating all iterators and references to it.

    This calls the removed element's destructor.
    I'll make some simple tests & see if my program crashes.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Vectors & auto-"delete"

    It deletes the object held and in your case this is a pointer and not the object pointed by the pointer. So before (or after) popping, you still need to call delete.

  6. The following user says thank you to wysota for this useful post:

    Darhuuk (11th January 2008)

  7. #5
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Vectors & auto-"delete"

    Ah ok, sorry, the explanation on that site threw me off. So it calls some kind of "pointer" destroyer, k. Thanks!

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Vectors & auto-"delete"

    No, it just frees the memory. Pointers don't have destructors.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.