Hi,
I have some questions on vector.
Please consider the code below. I use g++ compiler on Linux.

#include<iostream>
#include<vector>
using namespace std;
class Sampler {
      int a;
      int b;
};

int main()
{
         vector <int> vec1;
         vector<float> vec2;
         vector<Sampler> vec3;
         cout << sizeof(vec1) << " " << sizeof(vec2) 
                  << sizeof(vec3) << endl;
         
         return 0;
}

1. when I try sizeof() with vector objects of different types, the size is shown as 12 bytes. What surprises me even further is, even after pushing an element in to the vector, sizeof shows the same size.
Could somebody please explain why? Does the compiler not discriminate the type of data a vector holds?

2. In the above code snippet, when I define class Sampler as a local class, g++ shoots an error. Why is it that compiler works fine with vector vec3's declaration when class Sampler is global and shoots an error when Sampler is local to main?

Thanks in advance.

Recommended Answers

All 4 Replies

Member Avatar for jencas

1. in short: sizeof() returns the sum of the sizes of the member variables. One of these member variables points to the allocated space used for storing your data.

2. can't help you here, i'm using VS2008

The std::vectors uses heap storage (dynamic memory management). When you declare an vector, vec1, all you do is instantiate a vector object. When you push_back elements in a vector, it automatically manages it and store it somewhere in the memory and stores pointers to those object with it. So now when you say sizeof(vec), you are actually getting the size of the vector object and not of the elements inside them.
Hence, size of vector is constant (which differs from implementation to implementation) no matter how many element you push.
You would never need to bother about those.

Even now, if you are not clear, consider the following class:

class myVector
{
    private:
    int* p;
    public:
    myVector(int size)
    {
        int* p= new int[size];
    }
};

Now, no matter if you print sizeof(myVector(100)) or sizeof(myVector(500), the system will print the same value.
Why? Because sizeof() will yeild sizeof(int*) that is the size of a pointer to int [1].

[1] This is actually not true in general. sizeof() operator will actually yield a value which would be at least the value of the sum of the size of all its member. It may yield more than that too, in case polymorphism is involved, but we can leave the details here.

Edit1: To jencas: Read my foot note above. sizeof can also yield a value more than the individual members.

Edit2: To mrinal.s2008: Regarding your second problem, g++ is correct. According the the ISO C++, the template argument should have external linkage. Although, this rule is likely to be changed soon. Read http://gcc.gnu.org/ml/gcc/2009-04/msg00510.html

2. In the above code snippet, when I define class Sampler as a local class, g++ shoots an error. Why is it that compiler works fine with vector vec3's declaration when class Sampler is global and shoots an error when Sampler is local to main?

It is just one of those restrictions in the standard. Template type arguments cannot refer to a type without linkage, or a pointer to a type without linkage, and local class definitions match that restriction.

You can read the details in the draft, section 14.3.1.2.

Member Avatar for jencas

Edit1: To jencas: Read my foot note above. sizeof can also yield a value more than the individual members.

That's why I wrote "in short". Not only polymorphism can increase the size, memory boundary alignment can too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.