Boost logo

Boost :

Subject: [boost] [interprocess] How to know how much memory is taken up by an object allocated in boost interprocess shared memory?
From: nav999 (nkipe_at_[hidden])
Date: 2015-10-07 23:30:19

Re-posting, as the boost mailing list rejected the first post
mentioning that I had to add "[interprocess]" to the subject line.
I have asked this question on stackoverflow
, but got no response.

For this program:
#include <iostream>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/list.hpp>
#include <iostream>

#define SHARED_MEMORY_NAME "SO12439099-MySharedMemory"
#define DATAOUTPUT "OutputFromObject"
#define INITIAL_MEM 650000

using namespace std;
namespace bip = boost::interprocess;

class SharedObject
    unsigned int tNumber;
    bool pRcvdFlag;
    bool sRcvdFlag;
    unsigned long lTimeStamp;

typedef bip::allocator<SharedObject,
bip::managed_shared_memory::segment_manager> ShmemAllocator;
typedef bip::list<SharedObject, ShmemAllocator> SharedMemData;

int main()
        bip::managed_shared_memory* seg;
        SharedMemData *sharedMemOutputList;

        seg = new bip::managed_shared_memory(bip::create_only, DATAOUTPUT,
        const ShmemAllocator alloc_inst(seg->get_segment_manager());
        sharedMemOutputList =

        std::size_t beforeAllocation = seg->get_free_memory();
        std::cout<<"\nBefore allocation = "<< beforeAllocation <<"\n";
        SharedObject temp;
        std::size_t afterAllocation = seg->get_free_memory();
        std::cout<<"After allocation = "<< afterAllocation <<"\n";
        std::cout<<"Difference = "<< beforeAllocation - afterAllocation
        std::cout<<"Size of SharedObject = "<< sizeof(SharedObject) <<"\n";
        std::cout<<"Size of SharedObject's temp instance = "<< sizeof(temp)


The output is:

Before allocation = 649680
After allocation = 649632
Difference = 48
Size of SharedObject = 16
Size of SharedObject's temp instance = 16

If the size of SharedObject and it's instance is 16 bytes, then how can the
difference in allocation be 48? Even if padding had automatically been done,
it's still too much to account for 3 times the size (for larger structures
it goes to 1.33 times the size).
Because of this, I'm unable to allocate and dynamically grow the shared
memory reliably. If SharedObject contains a list which grows dynamically,
that could add to the uncertainty of space allocation even more.

How can these situations be safely handled? How can I know exactly how much
of memory to allocate?

View this message in context:
Sent from the Boost - Dev mailing list archive at

Boost list run by bdawes at, gregod at, cpdaniel at, john at