I'm looking for a definitive answer (if indeed one exists) on how much memory should be allocated when creating a static chunks of shared memory via boost::interprocess
's managed_shared_memory
. Even official examples seem to allocate arbitrarily large chunks of memory.
Consider the following structure:
// Example: simple struct with two 4-byte fields
struct Point2D {
int x, y;
};
My initial reaction is that the necessary size would be 8 bytes, or sizeof(Point2D)
. This fails miserably when I attempt to construct an object, giving me seg-faults at runtime.
// BAD: 8 bytes is nowhere near enough memory allocated.
managed_shared_memory segment(create_only, "My shared memory", sizeof(Point2D));
What read/write operation is causing seg-faults? Stack operations? Temporarily allocations within segment.construct()
? How much overhead is necessary when allocating shared memory?
By trial-and-error I found that multiplying the size by 4 can work for the above structure, but falls apart when I start adding more fields to my struct
. So, that reeks of a bad hack.
Some might argue that "memory is cheap" in the modern PC, but I disagree with this philosophy and dislike allocating more than I need to, if I can avoid it. I dug around the Boost docs yesterday and couldn't find any recommendations. Here's to learning something new today!