473,382 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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?
Aug 28 '08 #1
6 7731
On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.comwrote:
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.
Aug 28 '08 #2
On Aug 28, 9:37*pm, "C C++ C++" <m.azm...@gmail.comwrote:
On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.comwrote:
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?
Aug 28 '08 #3
On Aug 28, 2:47*pm, Lambda <stephenh...@gmail.comwrote:
On Aug 28, 9:37*pm, "C C++ C++" <m.azm...@gmail.comwrote:
On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.comwrote:
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.
Aug 28 '08 #4
On Aug 28, 2:47*pm, Lambda <stephenh...@gmail.comwrote:
On Aug 28, 9:37*pm, "C C++ C++" <m.azm...@gmail.comwrote:
On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.comwrote:
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
Aug 28 '08 #5
In article <4b**********************************@k30g2000hse. googlegroups.com>,
C C++ C++ <m.******@gmail.comwrote:
>On Aug 28, 2:47*pm, Lambda <stephenh...@gmail.comwrote:
>On Aug 28, 9:37*pm, "C C++ C++" <m.azm...@gmail.comwrote:
On Aug 28, 2:24*pm, Lambda <stephenh...@gmail.comwrote:
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?
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.
>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
Aug 28 '08 #6
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.comwrote:
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
Aug 28 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
3
by: steflhermitte | last post by:
Dear cpp-ians, I have a class: class mImage { public: //constructors / destructor mImage(unsigned int nrLayers); ~mImage();
10
by: ypjofficial | last post by:
Hello All, In my program I am using a pointer to a vector vector<XYZ> * vptr = new vector<XYZ>; and also the XYZ class has a char* as one of its member.I have created all the copy...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
4
by: jois.de.vivre | last post by:
I'm having some unexpected behavior regarding clear() in the map container. I have the following code: /// --- begin code #include <map> #include <vector> #include <iostream> #include...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
19
by: Krishanu Debnath | last post by:
Hello, I have a call to hash_map::clear() function which takes long time. someClass::someFunction() { // typedef hash_map<name_id, uintMp; // Mp p; // assuming proper namespace, hash...
17
by: toton | last post by:
Hi, I am using a vector to reserve certain amount of memory, and reuse it for new set of data, to avoid reallocation of memory. so the call is something like vector<my_datav;///the temporary...
2
by: Steve | last post by:
I have segmentation fault when calling resize on an stl vector. I'm unsure if I'm doing something horribly wrong or if the stl is being dumb. The code breaks down like this: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.