Interested in such a moment how much space does std :: vector int for 10x10 elements in x64 take?

for example, just std :: vector on 10 values ​​takes 24 bytes (the object itself) + 10x4 bytes (10 int values) = 64 bytes,

vector<int> a (10,1); 

accordingly 10x10 elements will occupy 24 + 24x10 + 10x10x4 = 664 bytes or 24 + 10x10x4 = 424 bytes?

 vector<vector <int>> и (10,vector <int> (10,1)); 

There was another question if I declared a vector

 vector<vector <int>> b (10,vector <int> (10)); 

but filled only one of its elements with a number, for example

 b[1][1] = 5; 

the remaining elements of the second vector will occupy 4 bytes, or until I assign them a value, then only the vector structure takes place? Respectively, the size is 24 + 24x10 + 4 = 268, or in any case there will be 664 bytes?

  • std::vector takes place both in the stack and in heap. According to the standard, it is guaranteed that it occupies O(n) memory in total, and I even think that n * sizeof (T) + O (1). But I think no one will give a guarantee for specific constants in O (1). - VladD
  • It depends on the implementation: in one version, the empty std :: string occupied 0x10 bytes from me, in the other - 0x1C, and in the debug build 0x20. With vectors most likely a similar situation. - Vladimir Martyanov

3 answers 3

N * N vector<vector<int>> ranked

 sizeof(vector) + sizeof(vector)*N + sizeof(int)*N*N + α(N) 

where α (N) is the overhead of memory allocation in the heap (the heap must be stored as much as allocated there)

sizeof (vector) is usually 3*sizeof(void*) , and it does not depend on the type that is stored in the vector. (A smaller vector is theoretically possible, but no one does this).

  • thanks, I thought so, I will try to do with a one-dimensional vector, well, or if it doesn't work, then I use this article habrahabr.ru/post/112826 - xttz
  • 2
    @xttz: Do you feel sorry for the unfortunate half kilobyte? You create your own problems. - VladD
  • @VladD I have an array of four-dimensional results (this is the number of parameters to calculate) and it will be stored in it at worst 32 million objects of 14 bytes each. the entire array size is> 400mb, so it’s not a good deal to spend 350kb on the structure, but only if the empty elements (which will definitely be) will not take up space - xttz
  • @xttz: 400 megabytes is not so much. Especially if you compile under a 64-bit platform. - VladD
  • It seems to me that the formula should look like this: sizeof(vector) + N*(sizeof(vector) + sizeof(int)*N) + α(N) , i.e., in fact, the difference is only in sizeof(int)*N*N , or am I mistaken somewhere? - StateItPrimitive

The vector size consists directly of the memory for the structure and the allocated memory for the array.

In the way you create vectors, their capacity will be fixed, that is, it is equal to the number of elements that you request. All memory will be allocated at once, naturally.

The exception is vector<bool> , which must hold the values ​​in bits, not bytes.

In the general case, as far as I remember, using only add operations, one can expect that the capacity of the vector exceeds the number of elements in it by no more than 2 times. When added and removed - 4 times.


http://codepad.org/mTvWj83Z

 #include <cstdio> #include <vector> int main(void) { vector < vector <int> > v(10, vector <int> (10,1)); printf("%d + %d*%d + %d*%d", sizeof v, v.capacity(), sizeof v[0], v[0].capacity(), sizeof v[0][0]); printf(" = %d\n", sizeof v + v.capacity() * sizeof v[0] + v[0].capacity() * sizeof v[0][0]); return 0; } 

Displays:

 28 + 10*28 + 10*4 = 348 

    The size of the memory occupied by the vector depends on the specific implementation of the class of the vector and the size of the value type. For example, the size of the int type can also vary depending on the environment where the program runs.

    If you run this test program

     #include <iostream> #include <vector> int main() { std::vector<std::vector<int>> v( 10, std::vector<int>( 10 ) ); size_t size1, size2, size3; std::cout << "sizeof( std::vector<std::vector<int>> ) = " << ( size1 = sizeof( std::vector<std::vector<int>> ) ) << std::endl; std::cout << "v.capacity() * sizeof( vector<int> ) = " << ( size2 = v.capacity() * sizeof( std::vector<int> ) ) << std::endl; std::cout << "v[0].capacity() * sizeof( int ) = " << ( size3 = v[0].capacity() * sizeof( int ) ) << std::endl; std::cout << "Total occupied memory size1 + size2 + 10 * size3 = " << size1 + size2 + 10 * size3 << std::endl; } 

    then the MS VC ++ online compiler produces the following values:

     sizeof( std::vector<std::vector<int>> ) = 12 v.capacity() * sizeof( vector<int> ) = 120 v[0].capacity() * sizeof( int ) = 40 Total occupied memory size1 + size2 + 10 * size3 = 532 

    While the gcc 5.2.0 compiler produces the following result:

     sizeof( std::vector<std::vector<int>> ) = 24 v.capacity() * sizeof( vector<int> ) = 240 v[0].capacity() * sizeof( int ) = 40 Total occupied memory size1 + size2 + 10 * size3 = 664 

    As you can see, even with the same size of type int size of the object itself of type std::vector different for different compilers.

    In VS VC ++, this size is 12 bytes, while in gcc 5.2.0 it is 24 bytes.

    • 3
      you have a VC ++ compiler for x32, and in the question about x64 - Abyx
    • one
      @Abyx It does not matter. The question of how the total memory occupied by the vector is determined. I have provided a test program that can be run on any platform. - Vlad from Moscow