Why does an object allocated in boost interprocess shared memory take up more memory than required?

For the below program using Boost interprocess shared memory,

#include <iostream> #include <boost/interprocess/mapped_region.hpp> #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/containers/list.hpp> #include <iostream> #define SHARED_MEMORY_NAME "SO12439099-MySharedMemory" #define DATAOUTPUT "OutputFromObject" #define INITIAL_MEM 650000 #define STATE_MATRIX_SIZE 4 using namespace std; namespace bip = boost::interprocess; class SharedObject { public: unsigned int tNumber; bool pRcvdFlag; bool sRcvdFlag; unsigned long lTimeStamp; }; typedef bip::allocator<SharedObject, bip::managed_shared_memory::segment_manager> ShmemAllocator; typedef bip::list<SharedObject, ShmemAllocator> SharedMemData; int main() { bip::managed_shared_memory* seg; SharedMemData *sharedMemOutputList; bip::shared_memory_object::remove(DATAOUTPUT); seg = new bip::managed_shared_memory(bip::create_only, DATAOUTPUT, INITIAL_MEM); const ShmemAllocator alloc_inst(seg->get_segment_manager()); sharedMemOutputList = seg->construct<SharedMemData>("TrackOutput")(alloc_inst); std::size_t beforeAllocation = seg->get_free_memory(); std::cout<<"\nBefore allocation = "<< beforeAllocation <<"\n"; SharedObject temp; sharedMemOutputList->push_back(temp); std::size_t afterAllocation = seg->get_free_memory(); std::cout<<"After allocation = "<< afterAllocation <<"\n"; std::cout<<"Difference = "<< beforeAllocation - afterAllocation <<"\n"; std::cout<<"Size of SharedObject = "<< sizeof(SharedObject) <<"\n"; std::cout<<"Size of SharedObject's temp instance = "<< sizeof(temp) <<"\n"; }//main

The output is:

Before allocation = 649680 After allocation = 649632 Difference = 48 Size of SharedObject = 16 Size of SharedObject's temp instance = 16

If the size of SharedObject and it's instance is 16 bytes, then how can the difference in allocation be 48? Even if padding had automatically been done, it's still too much to account for 3 times the size (for larger structures it goes to 1.33 times the size).
Because of this, I'm unable to allocate and dynamically grow the shared memory reliably. If SharedObject contains a list which grows dynamically, that could add to the uncertainty of space allocation even more.

How can these situations be safely handled?

Category:c# Views:4 Time:2018-10-02

Related post

  • C++ allocators, specifically passing constructor arguments to objects allocated with boost::interprocess::cached_adaptive_pool 2010-04-07

    This is an embarrassing question, but even the well-written documentation provided with boost.interprocess hasn't been enough for me to figure out how to do this. What I have is a cached_adaptive_pool allocator instance, and I want to use it to const

  • Why does the Array allocation my_arr[0,3] work while my_arr[3,0] fails? 2011-09-02

    I want to pull two values out from an array based on their index. Unfortunately this fails when the last index is zero and I don't undertand why. my_array = ["a", "b", "c", "d", "e", "f", "g"] my_array[1,2] # => ["b", "c"] my_array[0,2] # => ["

  • slow memory allocation in boost-interprocess 2011-10-03

    It looks like this: MyType * pMy = my_segment->construct<MyType>(anonymous_instance)(); my_segment->destroy_ptr(pMy); Where MyType is some typical struct and my_segment is correctly constructed boost::interprocess::managed_shared_memory *

  • why does the back arrow no longer allow you to jump back more than one page at a time? 2013-09-09

    why does the back arrow no longer allow you to jump back more than one page at a time? this is particularly frustrating on Facebook, where you can't always use the back arrow to get to your home page. --------------Solutions------------- Hi John, Rig

  • Why does Excel 2010 freeze when saving a shared document on our network? 2012-06-26

    Why does Excel 2010 freeze when saving a shared document on our network? We have a fairly simple, small excel document that is shared. It is located on one of our storage servers. All the users accessing this document have no problems with any other

  • Why does this Object wonk out & get deleted? 2010-04-10

    Stepping through the debugger, the BBox object is okay at the entry of the function, but as soon as it enters the function, the vfptr object points to 0xccccc. I don't get it. What is causing this ? Why is there a virtual table reference in there whe

  • Why does type knowledge disappear with Boost::MPL? 2011-05-01

    I have the following code and it works fine. #include <boost\mpl\vector.hpp> #include <boost\mpl\fold.hpp> #include <boost\mpl\for_each.hpp> #include <boost\mpl\inherit.hpp> #include <boost\mpl\inherit_linearly.hpp> #inc

  • why does this object declaration work in node.js and how to declare a static variable? 2012-01-16

    i have seen this code : var myNet = require ("net"); and in some function: function foo (x,y) { var myNewNet = new myNet(); myNewNet.createServer(x,y); } why does the code above create a new object? what is the mechanism stands behind that? one more

  • Why does the object passed to ModelAdmin.save_model not match the Model? 2012-01-27

    I am overriding the save_model method of my ModelAdmin so I can set some additional fields on the model. However, I keep getting a DoesNotExist exception. I stepped through the code with a debugger and I'm a bit confused. The model has this field: cr

  • Why does reading a file into memory takes 4x the memory in Java? 2009-07-06

    I have the following code which reads in the follow file, append a \r\n to the end of each line and puts the result in a string buffer: public InputStream getInputStream() throws Exception { StringBuffer holder = new StringBuffer(); try{ FileInputStr

  • Boost::Interprocess Shared Memory Bus Error 2010-06-23

    I'm using CentOS 5.4 x86_64 and Boost 1.42.0 on a cluster that uses Open-MPI 1.3.3. I'm writing a shared library that uses shared memory to store large amounts of data for multiple processes to use. There's also a loader application that will read in

  • boost::interprocess shared memory and R bigmemory 2010-09-08

    I have a server client system where the server is a vc++ program and the client is in R. Can I use a shared memory between these two? with boost::interprocess at the vc++ end and R bigmemory at the R end? the server and the client are on the same pc

  • boost interprocess : shared memory and stl types 2009-08-13

    I have a simple struct : struct MyType { std::string name; std::string description; } and I'm putting it in a shared memory : managed_shared_memory sharedMemory(open_or_create, "name", 65535); MyType* pType = sharedMemory.construct<MyType>("myT

  • Why does a PHP script constantly take up more memory? 2011-03-23

    See this example: echo memory_get_usage() . "\n"; // 36640 $a = str_repeat("Hello", 4242); echo memory_get_usage() . "\n"; // 57960 unset($a); echo memory_get_usage() . "\n"; // 36744 Can anyone explain why after un-setting the variable the memory us

  • Why does appending to TextBox.Text during a loop take up more memory with each iteration? 2012-01-04

    Short Question I have a loop that runs 180,000 times. At the end of each iteration it is supposed to append the results to a TextBox, which is updated real-time. Using MyTextBox.Text += someValue is causing the application to eat huge amounts of memo

  • Why does jQuery.get('/home/user/0/0a.html') fail when using file: rather than http:? 2010-07-05

    In the first two paragraphs a click produces an alert box with the content of the specified file. In the second two there is no alert box. <html><head> <script type="text/javascript" src="../../../../resources/jquery_1-4-2.js"></

  • Does functional programming take up more memory? 2010-12-23

    Warning! possibly a very dumb question Does functional programming eat up more memory than procedural programming? I mean ... if your objects(data structures whatever) are all imutable. Don't you end up having more object in the memory at a given tim

  • Why do apps take up more space than they indicate in the marketplace? 2013-11-18

    My phone is constantly running out of space so I decided to tabulate the amount of space that the games I have installed should take up. To do this I went through the list on the marketplace and wrote down how much space each app is supposed to use.

  • Why does this Objective C call appear to hang? 2011-12-13

    A friend of mine discovered some strange behaviour with NSDictionary, and I'm curious as to why it happens. Consider the following code: NSDictionary *dict = [[NSDictionary alloc] init]; // Oops, we can't mutate an NSDictionary [dict setObject:[[NSNu

  • Why does my object mocking fail? 2009-01-29

    I am using the Moq and can't seem to get my unit test to pass on what appears to be a simple mocking scenario. Product p = new Product(); var rep = new Mock<IProductRepository>(); rep.Expect(x => x.GetProductById(1)).Returns(p); p = rep.Obje

  • in C: Why does a stack allocated structure exist outside of the function? 2010-02-28

    my function: struct hostent * gethost(char * hostname){ if(/*some condition under which I want to change the mode of my program to not take a host*/){ return null } else{ struct hostent * host = gethostbyname(hostname); return host; } } in main: stru

  • Why does associated object not save? 2010-10-23

    I have a ruby (on rails) class: class User < ActiveRecord::Base # relationships belongs_to :current_shipping_address, :class_name => "Address" belongs_to :subscription # Validators validates_presence_of :subscription validates_presence_of :curr

  • Why does my object not have a constructor? 2010-12-01

    I am writing a few helper classes for scripting Adobe Illustrator. My problem starts in the Eyelet object. When I instantiate it it fails at the first new Group() because apparently Group doesn't have a constructor. Here is a stripped down version of

  • Why does an object field lose its array nature 2011-07-24

    I'm hitting a wall in PHP and could use your help. # php -v PHP 5.3.2 (cli) (built: Jun 4 2010 19:29:54) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies with the ionCube PHP Loader v3.3.20, Copyrigh

  • Why does this object need a DataContext? 2011-08-01

    This is weird. regiao C = new regiao(); C.Nome = textBoxNome.Text; C.Descricao = textBoxDescicao.Text; C.cidades.AddRange(ListaIN); mgrRegiao mgr = new mgrRegiao(); mgr.UpdateRegiao(C); C is a Linq object (code generated by VS2008 in the dbml file) t

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.125 (s). 11 q(s)