473,396 Members | 2,036 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,396 software developers and data experts.

std::vector::resize ,

On Page 67 of Effective STL, Meyers writes about resize: "If n is smaller
than the current size, elements at the end of the container will be
destroyed."

What does "destroyed" mean? It seems to me that if n is smaller than the
current size, simple adjust the internal size counter to n and be done.
Oct 17 '05 #1
10 5112
On Mon, 17 Oct 2005 16:21:59 -0700, "vsgdp" <sp**@void.com> wrote:
On Page 67 of Effective STL, Meyers writes about resize: "If n is smaller
than the current size, elements at the end of the container will be
destroyed."

What does "destroyed" mean? It seems to me that if n is smaller than the
current size, simple adjust the internal size counter to n and be done.


Destroyed means that the destructor for each element is called ... a
vector can have elements of class type as well as POD-type.

--
Bob Hairgrove
No**********@Home.com
Oct 17 '05 #2
vsgdp wrote:
On Page 67 of Effective STL, Meyers writes about resize: "If n is smaller
than the current size, elements at the end of the container will be
destroyed."
It means that the destructors for these objects are called.

What does "destroyed" mean? It seems to me that if n is smaller than the
current size, simple adjust the internal size counter to n and be done.


That works for POD types and types with trivial destructor. However, the
compiler is not required to optimize the destructor calls away.
Best

Kai-Uwe Bux
Oct 17 '05 #3

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:dj**********@murdoch.acc.Virginia.EDU...
vsgdp wrote:
On Page 67 of Effective STL, Meyers writes about resize: "If n is smaller
than the current size, elements at the end of the container will be
destroyed."
It means that the destructors for these objects are called.

What does "destroyed" mean? It seems to me that if n is smaller than the
current size, simple adjust the internal size counter to n and be done.


That works for POD types and types with trivial destructor. However, the
compiler is not required to optimize the destructor calls away.


Thanks. What does POD mean? I still do not understand. If you had a
vector of some class type C with vector size n, and resize to a new size m <
n, why must the destructors be called? You can just leave the objects
sitting there--the memory is still there. Then is you call push_back later
on, you are essentially doing an assignment to the next element, thereby
overwriting it. C's overloaded operator = or copy constructor should take
care of doing a correct assignment.
Best

Kai-Uwe Bux

Oct 18 '05 #4
vsgdp wrote:
Thanks. What does POD mean?
POD = Plain Old Data
I still do not understand. If you had a
vector of some class type C with vector size n, and resize to a new size m <
n, why must the destructors be called?


Because that's the way a container is defined in the Standard. If an
object is removed from a container, its destructor is called. And
resizing a vector to m, where m < n, is conceptually equivalent to
calling v.erase(v.begin() + m, v.end), or alternatively, calling
pop_back() n-m times.

Oct 18 '05 #5
vsgdp wrote:

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:dj**********@murdoch.acc.Virginia.EDU...
vsgdp wrote:
On Page 67 of Effective STL, Meyers writes about resize: "If n is
smaller than the current size, elements at the end of the container will
be destroyed."
It means that the destructors for these objects are called.

What does "destroyed" mean? It seems to me that if n is smaller than
the current size, simple adjust the internal size counter to n and be
done.


That works for POD types and types with trivial destructor. However, the
compiler is not required to optimize the destructor calls away.


Thanks. What does POD mean?


POD: plain old data

I still do not understand. If you had a vector of some class type C with vector size n, and resize to a new size m < n, why must the destructors be called? You can just leave the objects sitting there--the memory is still
there. Then is you call push_back later on, you are essentially doing an
assignment to the next element, thereby overwriting it. C's overloaded
operator = or copy constructor should take care of doing a correct
assignment.


If you want that, simply do not resize but overwrite the elements in the
vector. In C++ it is very important to *know* the lifetime of an object.
That is the reason that the standard says something. Whether you like what
the standard says or not is, of course, a different matter.
Best

Kai-Uwe Bux
Oct 18 '05 #6
I still do not understand. If you had a vector of some class type C with vector size n, and resize to a new size m < n, why must the destructors be
called? You can just leave the objects sitting there--the memory is
still
there. Then is you call push_back later on, you are essentially doing an
assignment to the next element, thereby overwriting it. C's overloaded
operator = or copy constructor should take care of doing a correct
assignment.


If you want that, simply do not resize but overwrite the elements in the
vector. In C++ it is very important to *know* the lifetime of an object.
That is the reason that the standard says something. Whether you like what
the standard says or not is, of course, a different matter.


I see. Okay, final question: is anything different for built in types? Say
if my std::vector contains ints? Do built in types have destructors?

Best

Kai-Uwe Bux

Oct 18 '05 #7
"vsgdp" <sp**@void.com> wrote in message
news:hRX4f.2875$i%.2572@fed1read07...
I see. Okay, final question: is anything different for built in types?
Say if my std::vector contains ints? Do built in types have destructors?


No.

-Mike
Oct 18 '05 #8
In article <71X4f.2865$i%.2742@fed1read07>, vsgdp <sp**@void.com> wrote:
What does POD mean?


Check out http://www.comeaucomputing.com/techtalk/#pod
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 18 '05 #9
Mike Wahler wrote:
"vsgdp" <sp**@void.com> wrote in message
news:hRX4f.2875$i%.2572@fed1read07...
I see. Okay, final question: is anything different for built in types?
Say if my std::vector contains ints? Do built in types have destructors?


No.


No they don't but I bet the vector implementation still calls the pseudo
destructor for them (of course the compiler most certainly optimizes
that away.

Oct 18 '05 #10

"vsgdp" <sp**@void.com> wrote in message
news:71X4f.2865$i%.2742@fed1read07...

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:dj**********@murdoch.acc.Virginia.EDU...
vsgdp wrote:
On Page 67 of Effective STL, Meyers writes about resize: "If n is
smaller
than the current size, elements at the end of the container will be
destroyed."


It means that the destructors for these objects are called.

What does "destroyed" mean? It seems to me that if n is smaller than
the
current size, simple adjust the internal size counter to n and be done.


That works for POD types and types with trivial destructor. However, the
compiler is not required to optimize the destructor calls away.


Thanks. What does POD mean? I still do not understand. If you had a
vector of some class type C with vector size n, and resize to a new size m
< n, why must the destructors be called? You can just leave the objects
sitting there--the memory is still there. Then is you call push_back
later on, you are essentially doing an assignment to the next element,
thereby overwriting it. C's overloaded operator = or copy constructor
should take care of doing a correct assignment.


Consider a class that handles it's own memory:

class MyClass
{
public:
MyClass() { MyData = new char[10000]; }
~MyClass() { delete MyData; }
// copy/assigment not show for brevity
// but should be here for the "rule of 3"
private:
char* Mydata;
};

Push that onto a vector... now, if the container gets resized and the
destructor is my called there is a memory leak, in this case at least 10,000
bytes of data. Do that a few times and you leak a lot of memory.

Class destructors must *always* be called if the constructor is called,
otherwise there are memory leaks.
Oct 19 '05 #11

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

Similar topics

4
by: enzo | last post by:
hi all, i don't understand what's wrong: 1) std::vector<double> p(10); doesn't compile:
0
by: solosnake | last post by:
Hello, and merry Christmas! When trying to compile some code that had specific alignment requirements, I found that the Visual Studio .NET compiler gave me compile time errors. It forbids...
1
by: Daniel J Watkins | last post by:
Hi, When I attempt to resize a column in a 2d vector I receive the error listed below. I'm using the libraries supplied with VxWorks. 0xf3fe450 (tExcTask): memPartFree: invalid block...
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike...
5
by: atreya | last post by:
Hi, I'm trying to figure out if there are any differences between the implementation of resize(0) and clear() that a programmer using std::vector should be aware of! Could there be any...
9
by: Gernot Frisch | last post by:
Hi, I want to be able to write: class foo { std::vector<intm_i (64); }
4
by: mathieu | last post by:
Hello, I am looking at the API of std::vector but I cannot find a way to specify explicitely the size of my std::vector. I would like to avoid vector::resize since it first initializes the...
3
by: Bram Kuijper | last post by:
Hi all, I am trying to resize a vector of objects (MyObj below), which contain references to other objects (OtherObj, see below). However, apparently somewhere in the resize operation an...
23
by: Mike -- Email Ignored | last post by:
In std::vector, is reserve or resize required? On: Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri Jul 27 18:10:34 EDT 2007 i686 athlon i386 GNU/Linux Using: g++ (GCC) 4.1.2 20070502 (Red Hat...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.