C++ sizeof Vector is 24?

前端 未结 1 1563
说谎
说谎 2020-11-29 07:57

I was just messing around and learning about vectors as well as structs, and at one point, I tried outputting the size of a vector in bytes. Here\'s the code:



        
相关标签:
1条回答
  • 2020-11-29 08:44

    While the public interface of std::vector is defined by the standard, there can be different implementations: in other words, what's under the hood of std::vector can change from implementation to implementation.

    Even in the same implementation (for example: the STL implementation that comes with a given version of Visual C++), the internals of std::vector can change from release builds and debug builds.

    The 24 size you see can be explained as 3 pointers (each pointer is 8 bytes in size on 64-bit architectures; so you have 3 x 8 = 24 bytes). These pointers can be:

    • begin of vector
    • end of vector
    • end of reserved memory for vector (i.e. vector's capacity)
    0 讨论(0)
提交回复
热议问题