-1

I am trying to understand what exactly is vector and it seems that it is very different from array. I tested the following simple code to see what it is storing

std::vector<int> A = { { 1, 2, 3 } };

If I query the memory of the vector with &A, I get the following values

e0 32 6a f7 f5 01 00 00 
00 27 6a f7 f5 01 00 00  
0c 27 6a f7 f5 01 00 00 
0c 27 6a f7 f5 01 00 00 

The second and third line is the start of value 1 and the next address of value 3 (the address for 3 is ...2708 to ...270B). What about the first and last line?

If I try with a vector array, the second and third line is no longer the starting and ending address of any values which I guess makes sense since the different vectors are not contiguous. But the question is what are the values stored in the vector in this case? and where is the address of the different vectors stored? The vector array is still 32 bytes. It doesn't seem like it can store the address of all the vectors.

I would greatly appreciate any assistance provided on this matter.

Did a quick search but can't seem to find anybody talking about this stuff. Is this specific to some compiler or IDE?

Edit: Don't understand why the other post answers my question. I read that post before posting this and while there is a reply that talks about some implementation of vector class, that is not all I am asking here. If vector stores the capacity, start and end pointer, how to explain why I can't find the address of any vectors in my vector array? If there is only 1 start and end pointer, how does it point to all the vectors in a vector array considering that the different vectors are not stored contiguously and the size of vector array is fixed??

  • 6
    Yes, it's compiler specific (or more precisely, standard library implementation specific). Typically, `std::vector` stores 3 pointers - to begin of allocated memory (which is also begin of data), to end of allocated memory and to end of data stored (alternatively, pointer to start of data and number of elements stored + number of elements that there is memory for). – Yksisarvinen Aug 28 '23 at 14:28
  • 2
    To find out what your specific compiler does, open the header file `vector` and start reading. – Raymond Chen Aug 28 '23 at 14:29
  • 2
    How did you get 32 bytes from it? Did you print more bytes than `sizeof`? – HolyBlackCat Aug 28 '23 at 14:29
  • @AndreySemashev Close, but none of the answers discusses the content of vector itself (the accepted answer only mentions one pointer). – Yksisarvinen Aug 28 '23 at 14:31
  • @HolyBlackCat I don't understand what you meant by that. I just use sizeof(A) and it shows 32 no matter if it is vector or vector array. – CoffeeBiscuit Aug 28 '23 at 14:37
  • 2
    What compiler is that? Just tried several different compilers, and all of them have 24 as sizeof. – HolyBlackCat Aug 28 '23 at 14:40
  • 2
    Usually `std::vector` takes 24 bytes (three pointers). But maybe your standard library implementation is different. – Fareanor Aug 28 '23 at 14:40
  • 4
    Those additional 8 bytes most likely come from some debug sanitation mechanism. They should go away in the release build. – Evg Aug 28 '23 at 14:50
  • 1
    MS STL: https://github.com/microsoft/STL/blob/main/stl/inc/xmemory#L1422-L1428 – Evg Aug 28 '23 at 14:56
  • It would help if you posted the code that confuses you - the "vector array" and explained what you expected. You can't find the addresses of vectors in the array because the array stores vectors, not pointers. The pointers to the vectors' elements are stored in the vectors. All elements are of the same size - `sizeof(std::vector)` - and stored contiguously. – molbdnilo Aug 28 '23 at 15:20
  • Note when reading Standard Library code that the implementation usually targets a specific compiler and often a constrained pool of hardware and OSes. When it does, the implementors know exactly how some undefined behaviours will manifest and will take advantage of that knowledge. Do not be surprised if you see code that is very risky, if not horribly illegal, under normal circumstances. – user4581301 Aug 28 '23 at 18:00
  • @molbdnilo The code would be simply vector> A = {{1,2},{3,4,5}}; and the problem is that none of the addresses stored in &A corresponds to &A[0][0] or &A[1][0] unlike the case in the post where the second address is the address of the first element – CoffeeBiscuit Aug 29 '23 at 00:50
  • @CoffeeBiscuit The second address *is* the address of the first element; the elements are `std::vector`s, not `int`s. Compare to `&A[0]` instead of `&A[0][0]`. – molbdnilo Aug 29 '23 at 05:48

0 Answers0