How does vector construct/destruct objects?

L

Lasse Skyum

I'm currently learning STL and I hate not knowing what is gooing on "inside"
STL... not because I really _need_ to know it to develop my game project,
but that's just my nature... like most of you at this grounp I suspect.

So the question is:

How does a std::vector construct and destruct the elements in it? I know it
has something to do with an allocator...

Suppose I wrote:
CMyClass *pDynArray = (CMyClass*)malloc(sizeof(CMyClass)*100);

Would it then be posible to construct/destruct the elements one by one in
that array?


I know that this constructs 100 elements at once:
CMyClass *pArray = new CMyClass[100];

.... but acording to my tests std::vector doesn't seem to use that aproach.
 
T

tom_usenet

I'm currently learning STL and I hate not knowing what is gooing on "inside"
STL... not because I really _need_ to know it to develop my game project,
but that's just my nature... like most of you at this grounp I suspect.

So the question is:

How does a std::vector construct and destruct the elements in it? I know it
has something to do with an allocator...

Suppose I wrote:
CMyClass *pDynArray = (CMyClass*)malloc(sizeof(CMyClass)*100);

Would it then be posible to construct/destruct the elements one by one in
that array?

Yes:

CMyClass initialValue(whatever);
for (int i = 0; i < 100; ++i)
{
//placement new just constructs at the location passed.
//Here we are copying an initialValue, just as vector does.
new(pDynArray + i) CMyClass(initialValue);
}

and destruction (order reversed for fun):

for (int i = 99; i >= 0; --i)
{
//direct destructor calls:
pDynArray.~CMyClass();
}

//and of course (unless you want to reuse the memory)
free(pDynArray);

std::allocator uses operator new and operator delete rather than
malloc and free.

That's essentially what goes on inside std::vector, although it
sometimes delegates to the functions allocator::construct (which does
placement new with a copy as above) and allocator::destroy (which just
calls the destructor as above). There is also the algorithm
uninitialized_fill, which essentially performs the loop above on an
iterator range.

Tom
 
L

Lasse Skyum

Thanks a lot! I'm getting somewhere now :)

--
Lasse
Right. It uses the specified (or default) allocator to do it. And the
default one has to use placement new and pseudo destructor calls.
Suppose I wrote:
CMyClass *pDynArray = (CMyClass*)malloc(sizeof(CMyClass)*100);

Would it then be posible to construct/destruct the elements one by one
in that array?

Yes. You can do:

#include <new>

// ...

for (int i = 0; i < 100; ++i)
{
new(pDynArray + i) CMyClass();
}

And later for destroying them:

for (int i = 0; i < 100; ++i)
{
pDynArray.~CMyClass();
}

Using an allocator, it looks like this:

std::allocator<CMyClass> a;

CMyClass* pDynArray = a.allocate(100);
CMyClass original;
for (int i = 0; i < 100; ++i)
a.construct(pDynArray + i, original);

This will copy construct the array objects from the original. And for
deletion:

for (int i = 0; i < 100; ++i)
a.destroy(pDynArray + i);
a.deallocate(pDynArray, 100);

I know that this constructs 100 elements at once:
CMyClass *pArray = new CMyClass[100];

... but acording to my tests std::vector doesn't seem to use that
aproach.
 
L

Lasse Skyum

Thanks Tom,

Just one thing left I don't understand then (about this subject, that is)
.... what happens then, when you use the basic types int,float,double,...
they don't have constructors/destructors(right?) so why don't vector<int>
make compile errors?.
 
L

Lasse Skyum

Damn I learning stuff today, thanks again!

I've been programming C++ for (say) 5 years now always using my custom
containers and making quite advanced 3D games... seems like I don't even
know the language I've been using! :-o
 
R

Rolf Magnus

Lasse Skyum said:
I'm currently learning STL and I hate not knowing what is gooing on
"inside" STL... not because I really _need_ to know it to develop my
game project, but that's just my nature... like most of you at this
grounp I suspect.

So the question is:

How does a std::vector construct and destruct the elements in it? I
know it has something to do with an allocator...

Right. It uses the specified (or default) allocator to do it. And the
default one has to use placement new and pseudo destructor calls.
Suppose I wrote:
CMyClass *pDynArray = (CMyClass*)malloc(sizeof(CMyClass)*100);

Would it then be posible to construct/destruct the elements one by one
in that array?

Yes. You can do:

#include <new>

// ...

for (int i = 0; i < 100; ++i)
{
new(pDynArray + i) CMyClass();
}

And later for destroying them:

for (int i = 0; i < 100; ++i)
{
pDynArray.~CMyClass();
}

Using an allocator, it looks like this:

std::allocator<CMyClass> a;

CMyClass* pDynArray = a.allocate(100);
CMyClass original;
for (int i = 0; i < 100; ++i)
a.construct(pDynArray + i, original);

This will copy construct the array objects from the original. And for
deletion:

for (int i = 0; i < 100; ++i)
a.destroy(pDynArray + i);
a.deallocate(pDynArray, 100);

I know that this constructs 100 elements at once:
CMyClass *pArray = new CMyClass[100];

... but acording to my tests std::vector doesn't seem to use that
aproach.
 
R

Rolf Magnus

Lasse Skyum said:
Thanks Tom,

Just one thing left I don't understand then (about this subject, that
is) ... what happens then, when you use the basic types
int,float,double,... they don't have constructors/destructors(right?)
so why don't vector<int> make compile errors?.

The compiler knows what to do to initialize the memory, since you gave
it the type. If the type is a class type, it means calling the
constructor, and for builtin types, it means just writing a value to
it. After all, you can write:

MyClass a(something);
int i(3);

even though int doesn't have a constructor. It just will initialize i
with the value 3. Doing that with dynamically allocated memory isn't
different:

MyClass* a = new MyClass(soemthing);
int* i = new int(3);

well, and with placement new, it's is of course also the same.
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top