#include #include #include #include #include #include #include #include #include class Test { public: Test *test; }; typedef boost::interprocess::basic_managed_mapped_file < char, boost::interprocess::rbtree_best_fit < boost::interprocess::null_mutex_family, boost::interprocess::offset_ptr >, boost::interprocess::null_index > memory_mapped_file; void* operator new(size_t size, memory_mapped_file &a) { return a.allocate(size); } int main() { using namespace boost::interprocess; //Remove shared memory on construction and destruction struct shm_remove { shm_remove() { std::remove("MySharedMemory"); } ~shm_remove(){ std::remove("MySharedMemory"); } } remover; Test *p; { //Create a managed shared memory memory_mapped_file shm(create_only, "MySharedMemory", 10000); //Check size std::cout << shm.get_size() << '\n'; p = new(shm) Test(); p->test = new(shm) Test(); //Check pointers std::cout << "before growing:" << '\n'; std::cout << "size: " << shm.get_size() << '\n'; std::cout << "p is owned: " << shm.belongs_to_segment(p) << '\n'; std::cout << "p->test is owned: " << shm.belongs_to_segment(p->test) << '\n'; std::cout << "p->test = " << p->test << '\n'; } { //Now that the segment is not mapped grow it adding extra 500 bytes memory_mapped_file::grow("MySharedMemory", 7*500); //Map it again memory_mapped_file shm(open_only, "MySharedMemory"); //Check pointers std::cout << "after growing" << '\n'; std::cout << "size: " << shm.get_size() << '\n'; std::cout << "p is owned: " << shm.belongs_to_segment(p) << '\n'; std::cout << "p->test is owned: " << shm.belongs_to_segment(p->test) << '\n'; std::cout << "p->test = " << p->test << '\n'; } return 0; }