Thread: deques, vectors and destructors.

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    deques, vectors and destructors.

    Just a quick question, stemming from my previous thread regarding the article about queues: When you 'erase' an element from a deque (or even vector), is it guaranteed that its destructor will be called immediately, or may the destruction be delayed until the container's internal sequence of elements is reallocated? i.e. if resources are released in the objects' destructors, then there could be unwanted side-effects if there is an indefinite delay before their destruction. I ask, simply because I can't think of an efficient and failsafe way to destroy single elements of an array (assuming that's how deques are implemented), and it would be inefficient to destroy/recreate all elements of the container each time a pop_back() or pop_front() occurs.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I don't believe the destruction can be delayed.

    Containers that use contiguous memory will use placement new for creation and call destructors explicity.

    gg

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I see. The use of placement new (on a buffer of type unsigned char* I assume), then, is to prevent multiple destructions when the memory needs reallocation?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Yep, something like that.

    That's also a reason for the varying iterator invalidation rules between container types.

    gg

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Cool, thanks!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed