It appears that the named allocation functions for the managed memory classes do not place constructed objects at the appropriate alignment for the requested type in all situations. Here is a simple example to illustrate the issue.
#include <iostream>
#include
// alignment of 16 on i386 and x86_64
struct vector_of_double
{
long double x;
long double y;
long double z;
};
int main()
{
namespace bip = boost::interprocess;
{
std::cout << "Alignment of vector_of_double is " << __alignof__(vector_of_double) << std::endl;
bip::managed_mapped_file simple_file_mapping(bip::create_only, "simple_mapping", 65536);
vector_of_double * test_vec = simple_file_mapping.construct("a named vector allocation")();
std::cout << "Resulting allocation is at address: " << test_vec << std::endl;
}
bip::remove_file_on_destroy("simple_mapping");
return 0;
}
On gcc targeting i386, this code prints:
Alignment of vector_of_double is 16
Resulting allocation is at address 0x???????c
Similar results on x86_64. It appears the resulting address is only aligned on a 4 byte boundary on 32 bit targets and 8 byte on 64 bit targets.
Are there workarounds? I have tried explicitly setting the alignment parameter in the mapped region's index parameter to 16, but it did not change the result.
I have tested the problem with anonymous allocations, and those do appear to be appropriately aligned. Only named allocations have the issue.