2

I try to do the following thing:

Create a "Big" Array ( 1 000 000 + Objects) into Shared memory with the boost::interprocess library

My Code contains the following:

managed_shared_memory testarray(create_only, "Test", 45000000);

typedef std::pair<SingleField, uint32_t> TestType;
TestType * test = testarray.construct<TestType>("TestArray")[45000000];

My question is: How do I figure out what the return type of this boost function is?

If i do the same above with the following : SingleField instead of "::pair it doesn't seems to work, but i dont need a second container, i only need one but with one it doesn't work!

The output of eclipse is somehow too cryptic for me. Since I work with boost I have been stopped several times because of such issues, is there an easy way to figure out what "Type" the function will give back? (I come from Java so I' am used to have some "Simple" definition which says Object x ) i would actually be happy if I could figure out the type which a specific function returns, with all the functions I write for myself this is simple but with this library I seem to have issues.

Second question: Why are those examples always with "type" pair, is it a library pre-condition?

-> I have tried using #include , Eclipse tells me its std::pair The question is why is it T* ? Is this the starting segment address?

Thanks for your time & answers.

Eclipse output:

Multiple markers at this line
- unused variable test
- cannot convert const 
 boost::interprocess::detail::named_proxy<boost::interprocess::segment_manager<char, 
 boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, 
 boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, Field, false> to 
 SharedMemoryInitializer::Create()::TestType* in initialization

I have read the boost Library manuals for several times, maybe i look at the wrong sites or pages and i would be pleased if you supply information which i miss.

http://www.boost.org/doc/libs/1_42_0/doc/html/interprocess/quick_guide.html#interprocess.quick_guide.qg_interprocess_container

Oliver
  • 928
  • 9
  • 25
  • I haven't used Boost.Interprocess before, but if you need to get the return type of a function in C++, you can always use `decltype` or `std::result_of`. See here for more info: http://stackoverflow.com/questions/2689709/difference-between-stdresult-of-and-decltype. – void-pointer Apr 07 '12 at 20:17
  • So this is basically what is used in the new c++ standard, i have tried using the keyword "auto" i guess my version is not up to the new c++ standard, i will give it a shot with decltype – Oliver Apr 07 '12 at 20:46
  • 1
    You may have to specify additional compilation flags for the compiler to use the latest standard. For g++-4.6, you can use `-std=c++0x`, and also `-std=c++11` in g++-4.7. – void-pointer Apr 08 '12 at 01:15
  • Thanks for telling me, unfortunately i have 4.5.x :( – Oliver Apr 08 '12 at 14:18
  • Unless you're forced to use `g++-4.5`, I'd highly recommend that you upgrade your compiler. The later versions of g++ come with better optimization capabilities, and the new C++11 features really make things a lot more convenient. That said, have you tried looking at the source code? – void-pointer Apr 08 '12 at 18:27
  • Hey void-pointer thanks for your constant support, i will see if i can upgrade the compiler, it seems to me that the error explains that it uses an "offset" pointer, as the normal ram alloc [obj1][obj2].. this array only seems to be an array of shared memory pointers but i want "real" shared memory objects – Oliver Apr 09 '12 at 15:22

1 Answers1

0

From my point of view, there are two major issues with your code:

  • you seem to be allocating 45000000 objects of type TestType which is probably not what you want (unless TestType only needs one byte per instance):

    TestType * test = testarray.construct<TestType>("TestArray")[45000000];

  • according to the documentation (*) you must provide provide parentheses for the constructor call (you are using the second version): TestType * test = testarray.construct<TestType>("TestArray")[45000000]();

    I assume that TestType has a parameter-less constructor.

(*) http://www.boost.org/doc/libs/1_46_1/doc/html/interprocess/managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.allocation_types

Andre
  • 1,577
  • 1
  • 13
  • 25
  • Thanks for your answer, the major mistake was the 45 000 000 , I thought it is bytes but it is actually "objects" – Oliver May 15 '12 at 05:09