By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,246 Members | 2,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,246 IT Pros & Developers. It's quick & easy.

How does vector construct/destruct objects?

P: n/a
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.
--
Lasse
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Lasse Skyum" <lskyum(AT)mail.dk> wrote:
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[i].~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.


Jul 19 '05 #2

P: n/a
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[i].~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.

Jul 19 '05 #3

P: n/a
On Tue, 28 Oct 2003 10:12:00 -0000, "Lasse Skyum" <lskyum(AT)mail.dk>
wrote:
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[i].~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
Jul 19 '05 #4

P: n/a
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?.

--
Lasse
Jul 19 '05 #5

P: n/a
"Lasse Skyum" <lskyum(AT)mail.dk> wrote:
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.

Jul 19 '05 #6

P: n/a
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

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

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.