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

How does vector construct/destruct objects?

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
6 4627
"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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: john smith | last post by:
Hi, when there is a vector<> of pointers to some objects, does calling resize cause vector to call delete on each object, or is there a memory leak problem? for example: struct base {//some...
35
by: Ram Laxman | last post by:
I have used vector in the VC++ compiler. I have included #include <string> #include <algorithm> #include <vector> std::vector<int> field; std::vector <int>::size_type i;
0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
10
by: gogogo_1001 | last post by:
Dear all, I don't understand why "delete" works well on destructing a object, but fails to destruct a vector of it. Any of your comment is highly appreciated! Following is the program...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
6
by: alan.patterson5 | last post by:
I have a vector of N values and a vector of N functors. I want to apply each functor to its corresponding value. What would be the 'correct' STL way to do this. for_each takes on functor and...
2
by: Jajjo | last post by:
Hey all, First time posting here and just a simple question to begin with. I'm trying to create an iterator for a vector of POINT objects and for some reason, I'm getting this error: error...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.