Question

I am trying to establish what data member primitives/objects (beyond the T* pointer) the boost (or std if its easier) shared_ptr has, which would be in the same L1 cache line as the T* pointer, when the shared_ptr is used to access the underlying heap memory?

When I do:

sizeof(boost::shared_ptr<int>)

and

sizeof(boost::shared_ptr<double>)

I get the same answer- 8 bytes, but on my system an int is 4 bytes and a double is 8 bytes?

I looked at the boost source code for shared_ptr:

http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/shared_ptr.htm

but I couldn't even see the T* data member.... so I am not overly sure how a boost::shared_ptr looks in terms of data members?

Was it helpful?

Solution

boost::shared_ptr<> contains two pointers, one to the object itself and one to the tracker object used for reference counting, proper destruction etc. That means it will be 2 * sizeof(void*) big.

The structure of the tracker object is of course an implementation detail, but it is a few dozen bytes large (virtual table, pointer to the object, reference count, weak count and some extra flags and whatnot).

If you use boost::make_shared() as recommended, both the object itself and the information helding one will be created in one heap block and the pointers will be just a few bytes apart.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top