clear() vs destructor?

Discussion in 'C++' started by Lambda, Aug 28, 2008.

  1. Lambda

    Lambda Guest

    For a std::vector, what's the difference between
    the clear() function and its destructor?

    I know their usages are different,
    but how about their implementations?
    Lambda, Aug 28, 2008
    #1
    1. Advertising

  2. Lambda

    C C++ C++ Guest

    On Aug 28, 2:24 pm, Lambda <> wrote:
    > For a std::vector, what's the difference between
    > the clear() function and its destructor?
    >
    > I know their usages are different,
    > but how about their implementations?



    clear is to clear the data but not the memory allocation.
    destructor clears everything.
    C C++ C++, Aug 28, 2008
    #2
    1. Advertising

  3. Lambda

    Lambda Guest

    On Aug 28, 9:37 pm, "C C++ C++" <> wrote:
    > On Aug 28, 2:24 pm, Lambda <> wrote:
    >
    > > For a std::vector, what's the difference between
    > > the clear() function and its destructor?

    >
    > > I know their usages are different,
    > > but how about their implementations?

    >
    > clear is to clear the data but not the memory allocation.
    > destructor clears everything.


    Do you mean:
    1. If the vector has 100 elements
    2. clear()
    3. Then the 100 memory spaces are not released, and
    all the 100 elements are uninitialized?
    Lambda, Aug 28, 2008
    #3
  4. Lambda

    C C++ C++ Guest

    On Aug 28, 2:47 pm, Lambda <> wrote:
    > On Aug 28, 9:37 pm, "C C++ C++" <> wrote:
    >
    > > On Aug 28, 2:24 pm, Lambda <> wrote:

    >
    > > > For a std::vector, what's the difference between
    > > > the clear() function and its destructor?

    >
    > > > I know their usages are different,
    > > > but how about their implementations?

    >
    > > clear is to clear the data but not the memory allocation.
    > > destructor clears everything.

    >
    > Do you mean:
    > 1. If the vector has 100 elements
    > 2. clear()
    > 3. Then the 100 memory spaces are not released, and
    > all the 100 elements are uninitialized?


    It will release all 100 but keep the minimum memory space required for
    vector.
    C C++ C++, Aug 28, 2008
    #4
  5. Lambda

    C C++ C++ Guest

    On Aug 28, 2:47 pm, Lambda <> wrote:
    > On Aug 28, 9:37 pm, "C C++ C++" <> wrote:
    >
    > > On Aug 28, 2:24 pm, Lambda <> wrote:

    >
    > > > For a std::vector, what's the difference between
    > > > the clear() function and its destructor?

    >
    > > > I know their usages are different,
    > > > but how about their implementations?

    >
    > > clear is to clear the data but not the memory allocation.
    > > destructor clears everything.

    >
    > Do you mean:
    > 1. If the vector has 100 elements
    > 2. clear()
    > 3. Then the 100 memory spaces are not released, and
    > all the 100 elements are uninitialized?


    "Calling clear() removes all elements from the controlled sequence.
    The memory allocated is not freed, however. All iterators become
    invalid, of course." - from codeguru dot com c++ article number c4027
    C C++ C++, Aug 28, 2008
    #5
  6. Lambda

    joseph cook Guest

    On Aug 28, 12:48 pm, (Yannick Tremblay) wrote:
    > In article <..com>,
    > C C++ C++ <> wrote:


    > Not sure what the standard says but my implementation does not release
    > any memory.  I am pretty sure it is not required by the satandard to
    > release the memory.  I don't know if it is allowed but I don't think I
    > have noticed an implementation that shrink the vector capacity.  Each
    > elements of the vector will however be destructed correctly when
    > clear() is called.


    In fact, the opposite is true. It is "required" that memory not be
    released. (in so far at least that the capacity() of the vector before
    and after a clear() will remain the same)

    Joe Cook
    joseph cook, Aug 28, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frs
    Replies:
    20
    Views:
    736
    Alf P. Steinbach
    Sep 21, 2005
  2. arun
    Replies:
    2
    Views:
    534
    benben
    Jun 13, 2006
  3. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    999
    Mark Fitzpatrick
    Jan 31, 2008
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    411
    Jimmy Hartzell
    May 19, 2008
  5. Jimmy Hartzell
    Replies:
    2
    Views:
    1,163
    Jimmy Hartzell
    May 20, 2008
Loading...

Share This Page