Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   clear() vs destructor? (http://www.velocityreviews.com/forums/t632751-clear-vs-destructor.html)

Lambda 08-28-2008 01:24 PM

clear() vs destructor?
 
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?

C C++ C++ 08-28-2008 01:37 PM

Re: clear() vs destructor?
 
On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.com> 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.

Lambda 08-28-2008 01:47 PM

Re: clear() vs destructor?
 
On Aug 28, 9:37*pm, "C C++ C++" <m.azm...@gmail.com> wrote:
> On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.com> 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?

C C++ C++ 08-28-2008 02:28 PM

Re: clear() vs destructor?
 
On Aug 28, 2:47*pm, Lambda <stephenh...@gmail.com> wrote:
> On Aug 28, 9:37*pm, "C C++ C++" <m.azm...@gmail.com> wrote:
>
> > On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.com> 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++ 08-28-2008 02:38 PM

Re: clear() vs destructor?
 
On Aug 28, 2:47*pm, Lambda <stephenh...@gmail.com> wrote:
> On Aug 28, 9:37*pm, "C C++ C++" <m.azm...@gmail.com> wrote:
>
> > On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.com> 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

joseph cook 08-28-2008 04:58 PM

Re: clear() vs destructor?
 
On Aug 28, 12:48*pm, ytrem...@nyx.nyx.net (Yannick Tremblay) wrote:
> In article <4b923fa1-75a4-45e0-b385-3674aecd0...@k30g2000hse.googlegroups..com>,
> C C++ C++ <m.azm...@gmail.com> 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


All times are GMT. The time now is 07:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.