About vector container in the STL (C++)

pascal_lalonde
pascal_lalonde used Ask the Experts™
on
Hello experts,

  I am programming in C++. I have a question the vector container of the STL.
Have a look at the code below.

  Why is the size of the structure line 24 bytes? I know there are 4 bytes for the
field nbPoints but where precisely the 20 others go (their meaning)?

Here is a fragment of the code:

typedef struct point
{
      int x;
      int y;
      int z;
} point;

typedef struct line
{
      int nbPoints;
      vector<point> points;
} line;

line myLine;
int sizeLine = sizeof(line);// 24 bytes
int sizeMyLine = sizeof(myLine);// 24 bytes


Thank you!


typedef struct point
{
	int x;
	int y;
	int z;
} point;
 
typedef struct line
{
	int nbPoints;
	vector<point> points;
} line;
 
line myLine;
int sizeLine = sizeof(line);// 24 bytes
int sizeMyLine = sizeof(myLine);// 24 bytes

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
sizeof(vector<point>) is likely 20 on your platform. There's not much more to think of that.
Top Expert 2009
Commented:
And if you're wondering why it's 20 - that's specific to your platform's specific implementation of the STL vector, but it likely contains a pointer to the data, a size, and some other information needed to implement all functionality of the vector.

Author

Commented:
I found the exact solution in the source code of the STL. Here is an excerpt:

*evilrix -- removed potentially copyrighted code *
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Top Expert 2009

Commented:
No, that's only true for your specific platform. On another platform, it might be implemented quite differently.

The internals of a vector are implementation dependent, and you should not rely on them.
Top Expert 2009

Commented:
Next time, if you want a platform specific answer, you should at least mention what platform you're using. Otherwise, the only correct answer is what I said in http:#25638827 :)
evilrixSenior Software Engineer (Avast)

Commented:
>> The answer {http:#25638804} and {http:#25638827} answer this question absolutely correctly.

And just to prove the point, this is the output when run on Linux when compiled with gcc:

sizeof(line) == 16
sizeof(myLine) == 16

#include <iostream>
#include <vector>
 
typedef struct point
{
	    int x;
	    int y;
	    int z;
} point;
 
typedef struct line
{
	    int nbPoints;
	    std::vector<point> points;
} line;
 
int main()
{
	 
	line myLine;
	size_t sizeLine = sizeof(line);
	size_t sizeMyLine = sizeof(myLine);
	
	std::cout
		<< "sizeof(line) == " << sizeLine << std::endl
		<< "sizeof(myLine) == " << sizeMyLine << std::endl;
}

Open in new window

Author

Commented:
I dont understand what you wrote, evilrix. I wanted to know the fields in vector class that occupy 20 bytes on my platform (16 on Linux). By looking into the source code of the STL, I can see their name and meaning even if their size is platform dependant. So I think the answers proposed by Infinit08 are not completes. Mine is not complete because even if I mentionned the name of the fields because I forgot to mention that their size is platform dependant. I proposed to split the points:
50 points for Inifinit08
50 points that I keep for me.

regards.

Author

Commented:
After reflexion, I think I will give all the points to Infinit08. Thank you to all of you!
Top Expert 2009

Commented:
>> By looking into the source code of the STL, I can see their name and meaning even if their size is platform dependant.

It's not just the size that is platform dependent. It's the whole implementation of the STL vector that is platform dependent. The only thing that is specified, is the interface (ie. the method prototypes you can call on the vector etc.) - how an STL library writer chooses to implement that is entirely up to him.

If you look at how an STL vector is implemented on a different platform, you'll very likely see quite a bit of differences, including the data members (amount, types, size, etc.).

Again : you cannot depend on the internals of the STL vector type.
evilrixSenior Software Engineer (Avast)

Commented:
>> So I think the answers proposed by Infinit08 are not completes
For the purposes of providing you a C++ Standards compliant answer it was fully complete, but I see you've reflected on that. Thanks.

Author

Commented:
Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial