Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 02:25 PM
Lambda
Guest
 
Posts: n/a
Default 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?
  #2  
Old August 28th, 2008, 02:45 PM
C C++ C++
Guest
 
Posts: n/a
Default Re: clear() vs destructor?

On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.comwrote:
Quote:
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.
  #3  
Old August 28th, 2008, 02:55 PM
Lambda
Guest
 
Posts: n/a
Default Re: clear() vs destructor?

On Aug 28, 9:37*pm, "C C++ C++" <m.azm...@gmail.comwrote:
Quote:
On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.comwrote:
>
Quote:
For a std::vector, what's the difference between
the clear() function and its destructor?
>
Quote:
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?
  #4  
Old August 28th, 2008, 03:35 PM
C C++ C++
Guest
 
Posts: n/a
Default Re: clear() vs destructor?

On Aug 28, 2:47*pm, Lambda <stephenh...@gmail.comwrote:
Quote:
On Aug 28, 9:37*pm, "C C++ C++" <m.azm...@gmail.comwrote:
>
Quote:
On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.comwrote:
>
Quote:
Quote:
For a std::vector, what's the difference between
the clear() function and its destructor?
>
Quote:
Quote:
I know their usages are different,
but how about their implementations?
>
Quote:
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.
  #5  
Old August 28th, 2008, 03:45 PM
C C++ C++
Guest
 
Posts: n/a
Default Re: clear() vs destructor?

On Aug 28, 2:47*pm, Lambda <stephenh...@gmail.comwrote:
Quote:
On Aug 28, 9:37*pm, "C C++ C++" <m.azm...@gmail.comwrote:
>
Quote:
On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.comwrote:
>
Quote:
Quote:
For a std::vector, what's the difference between
the clear() function and its destructor?
>
Quote:
Quote:
I know their usages are different,
but how about their implementations?
>
Quote:
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
  #6  
Old August 28th, 2008, 05:55 PM
Yannick Tremblay
Guest
 
Posts: n/a
Default Re: clear() vs destructor?

In article <4b923fa1-75a4-45e0-b385-3674aecd0905@k30g2000hse.googlegroups.com>,
C C++ C++ <m.azmath@gmail.comwrote:
Quote:
>On Aug 28, 2:47*pm, Lambda <stephenh...@gmail.comwrote:
Quote:
>On Aug 28, 9:37*pm, "C C++ C++" <m.azm...@gmail.comwrote:
>>
Quote:
On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.comwrote:
>>
Quote:
For a std::vector, what's the difference between
the clear() function and its destructor?
>>
Quote:
I know their usages are different,
but how about their implementations?
>>
Quote:
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?
Correct (although to be precise all the 100 elements are destructed)
The vector itself is not destructed and the memory used by the vector
is not released.
Quote:
>It will release all 100 but keep the minimum memory space required for
>vector.
No

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.

Try a little example program such as:

#include <vector>
#include <iostream>

struct SomeType
{
SomeType()
{
std::cout << "SomeType constructor for " << this << std::endl;
};
~SomeType()
{
std::cout << "SomeType destructor for " << this << std::endl;
};
SomeType(SomeType const & )
{
std::cout << "SomeType copy constructor for " << this <<
std::endl;
};
};

int main()
{
std::vector<SomeTypetheVector(10);
std::cout << "Vector done\n";
std::cout << "Capacity " << theVector.capacity() << std::endl;
std::cout << "Size: " << theVector.size() << std::endl ;
std::cout << "Calling clear() \n";
theVector.clear();
std::cout << "Capacity " << theVector.capacity() << std::endl;
std::cout << "Size: " << theVector.size() << std::endl ;
return 0;
}


Yan
  #7  
Old August 28th, 2008, 06:05 PM
joseph cook
Guest
 
Posts: n/a
Default Re: clear() vs destructor?

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

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles