473,769 Members | 7,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on use of "placement" new

Hi,

We have a class whose objects are to be allocated in shared memory. To
do that a ShmManager base class is defined so that operator new and
delete are redefined to allocate segments in shared memory. A typical
class "Foo" then inherit from ShmManager to have get this behaviour.
class ShmManager {

void* operator new(size_t size);
void operator delete(void* p, size_t size);

};

class Foo : public ShmManager
{

int fData1;
Barr fData2[16];

};

In the previous Foo example, the size of fData2 array is known at
compilation time, but we need to make this size "dynamic", but keeping
the object memory layout "flat".

We are using the "placement" new syntax doing:

class Foo1 : public ShmManager
{

int fData1;
Barr fData2[]; // will be extented using "placement" new

};

ShmManager* ptr = ShmManager::ope rator new(sizeof(Foo1 ) + num *
sizeof(Barr));
Foo1* = new(ptr) Foo1();

So that Foo1 object nows gets a dynamic "num" number of elements. This
seems to work, but is this safe to do that? Are we obliged to put the
fData2 fied as the *last* element in the Foo1? Is there any better/
safer manner to implement the same pattern?

Thanks

Stéphane Letz
Jun 27 '08
11 1775
Frank Birbacher wrote:
Hi!

Ian Collins schrieb:
>You don't need boost for that, all you have to do is provide an
appropriate allocator to manage a memory pool in shared memory.

Right. My point was: it works, you can use vectors in shared memory. The
OP seems to oppose to this solution. I cannot agree on some fragile
solution using the variable-sized-object-and-int[]-hack (which is
illegal in C++) when there is a cleaner solution (using a vector with
shmem alloc).
I agree. The only real taboos with hared memory objects are static data
members and virtual functions.

--
Ian Collins.
Jun 27 '08 #11
On 19 Maj, 01:01, Ian Collins <ian-n...@hotmail.co mwrote:
Frank Birbacher wrote:
Hi!
Paavo Helde schrieb:
I guess with this you meant to say that e.g. std::vector is outlawed.
Well, boost allows vectors, maps, and other stuff in shared memory:
http://www.boost.org/doc/libs/1_35_0...ess/quick_guid...

You don't need boost for that, all you have to do is provide an
appropriate allocator to manage a memory pool in shared memory.
This requires the memory pool to have the same adress in every
process. Apparantly this is not the case for the OP.

/Peter
Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
1927
by: Istvan Albert | last post by:
Paul McGuire wrote: > Please reconsider the "def f() :" construct. Instead of > invoking a special punctuation character, it uses context and placement, > with familiar old 's, to infuse the declaration of a function with special > characteristics. If this causes def lines to run longer than one line, > perhaps the same rule that allows an unmatched "(" to carry over multiple > lines without requiring "\" continuation markers could be...
5
3176
by: Mysooru | last post by:
How to restrict an object to be instantiated only using new operator? I mean, one should not be allowed to create an object like AnObject obj; it should be AnObject obj = new AnObject;
10
2617
by: Audun Røe | last post by:
Hi, I have a list of codes which I want translated into something "understandable". Is there a mechanism such as hashtables that could handle this? (eg. PHP: array('F' => 'Foo', 'C' => 'Cat')) It just seems as if a page worth of if-tests would be a fairly clumsy solution to this (there are a lot of codes) problem. Basically, I have an XML-document which looks like something like
4
2881
by: michaaal | last post by:
I have two folders in my website... Folder1 (this is where my #include file is, this is where the style.css is) --Folder2 (Folder2 is inside of Folder1) Folder2 contains a file that has this #include statement... <!--#include file="../include.inc"-->
8
2475
by: Ryan R. Rosario | last post by:
Hi - **I apologize for posting this again. I dont think my first one went thru** I am in the process of converting a table based website over to a CSS based website and I am having a problem. The website "template" consists of 3 regions: header, sidebar and content. Header spans horizontally across the screen at the top of the
18
8085
by: Leslaw Bieniasz | last post by:
Cracow, 28.10.2004 Hello, I have a program that intensively allocates and deletes lots of relatively small objects, using "new" operator. The objects themselves are composed of smaller objects, again allocated using "new". From my tests I deduce that a considerable part of the computational time is spent on the memory allocation, which makes the program substantially slower compared to the equivalent code written using
5
2034
by: Mark P | last post by:
I think my previous posts on this subject are too verbose and are scaring off replies so I'll keep this one brief: If I define my own operator new(size_t size) in a class, does that automatically hide (global) placement new? Would this prevent STL container classes from working (e.g., does std::allocator require placement new for its construct() method?)? Thanks for your help,
2
4597
by: rowe_newsgroups | last post by:
Hello all, When the user clicks the Restore button on the taskbar, how can I set what size the form restores to? I would like to have it "remember" what the last size was, like most windows apps do. Note - I use a XML config file to track the user settings. Any advice is welcome, Seth Rowe
5
3346
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.