Wonter
Wonter

Reputation: 343

A puzzle about boost::interprocess::managed_shared_memory->size

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?

Upvotes: 1

Views: 431

Answers (1)

sehe
sehe

Reputation: 393769

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

Upvotes: 4

Related Questions