Search code examples
c++c++11shared-ptrsmart-pointers

How much memory do 1000 shared pointers take?


  1. How much memory do (e.g.) 1000 shared pointers take?
  2. is it 16 x 1000 bytes?
  3. Does it differ for 32 and 64bit system?

Solution

  • In a typical implementation, std::shared_ptr holds only two pointers.

    So 1000 shared pointers take up 1000 * 2 * sizeof(pointer) bytes of memory.

    • Size of a pointer is 4 bytes on all 32-bit systems that follow ILP32 data model.
    • Size of a pointer is 8 bytes on a 64-bit systems that follow LP64 data model (Most Unix and Unix-like systems) or LLP64 data model (Microsoft Windows x86-64).

    Note: The size of the control block (which is implementation dependent) and the size of the object the shared pointer shares ownership of are not part of this.