1

here is the class

class A{
public:
    vector<int> vvv1{1};
};

class B{
public:
    vector<int> vvv2{1,2,3,4,5};
};

and the main

int main(){
    A a;/*sizeof(a) == 24;*/
    B b;/*sizeof(b) == 24;*/
    return 0;
}

why the size of a and the size of b are both 24?

ZQL_NINE
  • 19
  • 1
  • 1
    `sizeof(std::vector)` is *not* the same thing as `std::vector::size()`/`std::size(std::vector)`. The first gives you the amount of bytes occupied by the type. The other two give you the number of contained objects/elements. Additionally `std::vector::capacity()` can tell you how many elements it can currently store before reallocation/growth is needed. – Jesper Juhl Sep 13 '18 at 15:43
  • Remember `sizeof()` is a compile time constant. It only measures the size of the object not the size of any memory that the object dynamically allocates. – drescherjm Sep 13 '18 at 15:48
  • thanks a lot. I'm thinking what the difference between vector vec and new vector since vector is dynamically allocated. – ZQL_NINE Sep 13 '18 at 16:04
  • `new vector` creates a dynamically allocated vector. The difference would be the object itself would be dynamically allocated so in your case all 24 bytes would be on the free store as well. Note: It does not make a lot of sense to use this in most cases. – drescherjm Sep 13 '18 at 16:10
  • you are so nice for answer my questions.So that mines when I use new vector, I just allocate 24Bytes from the heap, the contents of the vector are always dynamic allocated. What's more,when I use vector *v = new vector, v = 0x71dcf0. When I use vector vec, &vec[0] = 0x7fd2e10a4010,what's the difference between two address.My computer is 64 bit. – ZQL_NINE Sep 13 '18 at 16:29
  • ***I just allocate 24Bytes from the heap*** this part is not correct. You allocate 24 bytes on the free store + the amount the vector allocates to store your items. – drescherjm Sep 13 '18 at 16:33
  • ***what's the difference between two address*** Addresses don't have to be in the same range. This is a function of your OS + heap implementation + the amount of fragmentation of your heap. – drescherjm Sep 13 '18 at 16:35
  • thanks a lot for you nicely answer. – ZQL_NINE Sep 13 '18 at 16:59

2 Answers2

1

sizeof() gives you the number of bytes in memory that the object is occupying. The class std::vector is a container that has its own member variables to manage the internal array that it is representing, and they are counted as well as part of the size. Both a and b in your case are too small in number of elements to make it reallocate its internal array to hold more than what it initially uses to hold a single element.

For illustration, my compiler returns 32 for both these cases:

#include <vector>

int main()
{
    std::vector<int> a{ 1 };
    std::vector<int> b{ 1,2,3,4,5 };

    int sizeA = sizeof(a); // Returns 32
    int sizeB = sizeof(b); // Returns 32

    return 0;
}
Geezer
  • 5,600
  • 18
  • 31
0

The size of the contents of the vector does not change the size of the class. A vector object will always take up 24 bytes with the std lib implementation that you’re currently using. To be a dynamic array, the vector allocates memory on the fly to hold its contents. What’s actually in the vector class is the capacity of the vector, it’s current size, and a pointer to the data. Class sizes are always static

Gabriel
  • 829
  • 5
  • 12
  • thanks a lot. I'm thinking what the difference between vector vec and new vector since vector is dynamically allocated. – ZQL_NINE Sep 13 '18 at 16:05
  • The vector class itself is not dynamically allocated. The vector uses dynamic allocation to store its data. Using `new vector` will alllocate from the heap and require you to `delete` it later. Otherwise, they'll function the exact same since they are the same class. You just allocated the memory used for the class differently. – Gabriel Sep 13 '18 at 16:09
  • So that mines when I use new vector, I just allocate 24Bytes from the heap, the contents of the vector are always dynamic allocated. When I use vector *v = new vector, v = 0x71dcf0. When I use vector vec, &vec[0] = 0x7fd2e10a4010,what's the difference between two address.My computer is 64 bit. – ZQL_NINE Sep 13 '18 at 16:24
  • 1
    I think you're confused as to what I meant by contents. I don't mean the member variables. I mean the array is dynamically allocated to allow for a dynamic array. `vector` calls `new int[whateverSizeItNeeds]` when it needs store `int`s. The vector itself (i. e. the capacity, size, and pointer to the array) is not dynamically allocated unless you explicitly do so with `new` – Gabriel Sep 13 '18 at 16:38
  • thank you, I think I get your point now. – ZQL_NINE Sep 13 '18 at 16:53