std::vector::resize ,

V

vsgdp

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.
 
K

Kai-Uwe Bux

vsgdp said:
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
 
B

Bob Hairgrove

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.
 
V

vsgdp

Kai-Uwe Bux said:
It means that the destructors for these objects are called.



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.
 
R

red floyd

vsgdp said:
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.
 
K

Kai-Uwe Bux

vsgdp said:
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
 
V

vsgdp

vector size n, and resize to a new size m < n, why must the destructors be

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?
 
M

Mike Wahler

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
 
R

Ron Natalie

Mike said:

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.
 
J

Jim Langston

vsgdp said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top