1

i have two programs.

#include <iostream>

#include <boost/interprocess/managed_shared_memory.hpp>


int main(int argc, char const* argv[])
{
    boost::interprocess::shared_memory_object::remove("High");
    try {
        boost::interprocess::managed_shared_memory managed_shm(
                boost::interprocess::create_only,
               "High",
                256);
        std::cout << "success" << std::endl;
    }
    catch (boost::interprocess::interprocess_exception &ex) {
        std::cout << ex.what() << std::endl;
    }
    return 0;
}

It prints the output "boost::interprocess_exception::library_error"

But changing 256 to 512, it prints "success":

#include <iostream>

#include <boost/interprocess/managed_shared_memory.hpp>


int main(int argc, char const* argv[])
{
    boost::interprocess::shared_memory_object::remove("High");
    try {
        boost::interprocess::managed_shared_memory managed_shm(
                boost::interprocess::create_only,
               "High",
                512);
        std::cout << "success" << std::endl;
    }
    catch (boost::interprocess::interprocess_exception &ex) {
        std::cout << ex.what() << std::endl;
    }
    return 0;
}

what is the difference between 256 and 512?

sehe
  • 374,641
  • 47
  • 450
  • 633
Wonter
  • 293
  • 1
  • 5
  • 15
  • 1
    Can you provide more information about platform / compiler / boost version? I just ran your code on Visual Studio 2010 with boost 1.55 and with boost 1.62, and the output was "success" with both 256 and 512 shared memory size. – pschill Jun 01 '17 at 11:16
  • @pschill likely on 32 bit targets – sehe Jun 01 '17 at 12:12
  • 1
    No i tried both 32 and 64 targets. But right now I retried other memory sizes and on 64bit it works with size 256, but crashes with size 255 :) – pschill Jun 01 '17 at 12:17

1 Answers1

4

The difference between 256 and 512 is 256.

256 bytes is too small for a segment manager control block on your system (likely, any 64-bit target).

The overhead might surprise you but it makes a bit of sense because there's "heap-management" involved (it's called segment management in Boost Interprocess).

See also Bad alloc is thrown:

There's considerable initial overhead, consuming 320 bytes before anything happened.

(It also shows a graph of different allocation schemes/data structures)

If you want a raw shared memory object, without segment management, use shared_memory_object

sehe
  • 374,641
  • 47
  • 450
  • 633