473,756 Members | 5,595 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 #1
11 1773
le**@grame.fr wrote in news:dd732c4c-3cd7-4319-ae60-be2cb2a487a6
@d77g2000hsb.go oglegroups.com:
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".
I guess with this you meant to say that e.g. std::vector is outlawed.
>
We are using the "placement" new syntax doing:

class Foo1 : public ShmManager
{

int fData1;
Barr fData2[]; // will be extented using "placement" new
This is not valid C++. This is valid syntax in C and some C++ compilers
might accept this as an extension. AFAIK it is not planned to include
this feature in C++0x as in C++ there are better means for dynamically
sized arrays (std::vector, ...). Thus in C++ you are obliged to declare
it as Barr fData2[1]; however, accessing elements other than 0 is
undefined behavior (which might happen to be defined by your particular
implementation) . In practice I suspect that your code works on most
mainstream implementations , but this is not guaranteed by the standard.
};

ShmManager* ptr = ShmManager::ope rator new(sizeof(Foo1 ) + num *
sizeof(Barr));
Foo1* = new(ptr) Foo1();
Foo1* p_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?
Sure. And better not derive from that class.
Is there any better/
safer manner to implement the same pattern?
Using C?

One option would be to let the fData2 out of the class definition,
allocate memory as you do now, and obtain the Barr array pointer by
something like:

Barr* barr = static_cast<Bar r*>( static_cast<voi d*>(p_foo1+1));

Provided that the alignment requirements for Barr are the same or less
stringent than for Foo1, I think this should be kosher, I'm sure others
will correct me if I'm wrong.

hth
Paavo
Jun 27 '08 #2
On 17 Maj, 18:15, l...@grame.fr wrote:
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".
Why must it be flat? You can't do that portably in C++ and there are
potential problems here - e.g. if Barrs alignment is not compatible
with that of int.
I will assume that a solution where fData2 is a pointer to memory not
adjacent to fData1.
>
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();
There is no reason to inherit from ShmManager here. This would have
been necessary only in the case where you would be able to new Foo1
directly.
>
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?
There are problems:
* your code will have 16 calls to the Barr constructor and this is
only correct when num is 16. To solve this problem, you need to extend
your code.
* The alignment problems mentioned before.
* The nonportability. The code above is not valid C++, and you might
get into troubles for that.

If the requirement was that only the fData2 elements would have to be
contigious, much better solutions exist. Your solution is nonportable
and fragile, but sometimes you just might have to live with that.

/Peter
Jun 27 '08 #3
On 17 mai, 19:49, peter koch <peter.koch.lar ...@gmail.comwr ote:
On 17 Maj, 18:15, l...@grame.fr wrote:
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".

Why must it be flat?
Remerber the object lives in shared mem, then having it "flat" greatly
simplify it's use: any process that needs it gets the base object
pointer and can access any field "directly".
You can't do that portably in C++ and there are
potential problems here - e.g. if Barrs alignment is not compatible
with that of int.
I will assume that a solution where fData2 is a pointer to memory not
adjacent to fData1.
If we switch to a solution where fData2 a allocated elsewhere, (that
is in another shared memory segment in our case) then we have to deal
with more complex memory access scheme, again because of the shared
memory issue.
>

If the requirement was that only the fData2 elements would have to be
contigious, much better solutions exist. Your solution is nonportable
and fragile, but sometimes you just might have to live with that.
The whole point of the initial question was: how to allow to have a
dynamic sized array as a field *and* keep the "flat" acess property.
What kind of "much better solutions exist" are you thinking of?

Thanks

Stephane Letz
Jun 27 '08 #4
On 17 Maj, 20:17, l...@grame.fr wrote:
On 17 mai, 19:49, peter koch <peter.koch.lar ...@gmail.comwr ote:


On 17 Maj, 18:15, l...@grame.fr wrote:
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".
Why must it be flat?

Remerber the object lives in shared mem, then having it "flat" greatly
simplify it's use: any process that needs it gets the base object
pointer and can access any field "directly".

*You can't do that portably in C++ and there are
potential problems here - e.g. if Barrs alignment is not compatible
with that of int.
I will assume that a solution where fData2 is a pointer to memory not
adjacent to fData1.

If we switch to a solution where fData2 a allocated elsewhere, (that
is in another shared memory segment in our case) then we have to deal
with more complex memory access scheme, again because of the shared
memory issue.
If the requirement was that only the fData2 elements would have to be
contigious, much better solutions exist. Your solution is nonportable
and fragile, but sometimes you just might have to live with that.

The whole point of the initial question was: how to allow to have a
dynamic sized array as a field *and* keep the "flat" acess property.
What kind of "much better solutions exist" are you thinking of?
I believe now that you are in a situation where you have a piece
memory that is shared between different processes but where the adress
of each segment is different from process to process. In that case, I
believe there is not a better solution than what you proposed.
If the adress is the same in each process, I would use a std::vector
with an allocator allocating from ShmMem.

/Peter
Jun 27 '08 #5
On 17 mai, 20:31, peter koch <peter.koch.lar ...@gmail.comwr ote:
On 17 Maj, 20:17, l...@grame.fr wrote:
On 17 mai, 19:49, peter koch <peter.koch.lar ...@gmail.comwr ote:
On 17 Maj, 18:15, l...@grame.fr wrote:
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".
Why must it be flat?
Remerber the object lives in shared mem, then having it "flat" greatly
simplify it's use: any process that needs it gets the base object
pointer and can access any field "directly".
*You can't do that portably in C++ and there are
potential problems here - e.g. if Barrs alignment is not compatible
with that of int.
I will assume that a solution where fData2 is a pointer to memory not
adjacent to fData1.
If we switch to a solution where fData2 a allocated elsewhere, (that
is in another shared memory segment in our case) then we have to deal
with more complex memory access scheme, again because of the shared
memory issue.
If the requirement was that only the fData2 elements would have to be
contigious, much better solutions exist. Your solution is nonportable
and fragile, but sometimes you just might have to live with that.
The whole point of the initial question was: how to allow to have a
dynamic sized array as a field *and* keep the "flat" acess property.
What kind of "much better solutions exist" are you thinking of?

I believe now that you are in a situation where you have a piece
memory that is shared between different processes but where the adress
of each segment is different from process to process. In that case, I
believe there is not a better solution than what you proposed.
If the adress is the same in each process, I would use a std::vector
with an allocator allocating from ShmMem.

/Peter
I finally found this : http://www.devmaster.net/forums/showthread.php?t=11310
that basically does what I what..
.
Any comments?

Thanks

Stephane

Jun 27 '08 #6
On 17 mai, 18:15, l...@grame.fr wrote:
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".
Formally, it can't be done. Practically, see below. (I'm
assuming the Barr is a POD type. Otherwise, you'll run into any
number of problems.)
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();
That's legal C99, but not legal C++. In C++, it would cause
some additional problems (e.g. if you inherit from ShmManager).
To date, no one has done the work necessary to solve them, so it
probably won't be adopted into C.

The way I've worked around this in the (distant) past is to
define something like:

class Foo : public ShmManager
{
int fData1 ;
Barr* fData2() { return this + 1 ; }

public:
void* operator new( size_t n, size_t elementCount )
{
return ShmManager::ope rator new(
n + elementCount * sizeof( Barr ) ) ;
}
} ;

Note, however, that this may create problems with alignment.
In my case, Barr was in fact char, so the problem didn't
occur. If Barr is something more complicated, you'll have to
take additional steps to ensure that sizeof( Foo ) is a multiple
of the alignment needed for Barr.

Note that this requires the client code to use a somewhat
special syntax:

new ( n ) Foo ;

and that it only really works if Barr is a POD (but my
experience is that it's best to stick with POD's in shared
memory anyway).
So that Foo1 object nows gets a dynamic "num" number of
elements. This seems to work, but is this safe to do that?
It's not legal to specify an empty array specifier here, and if
you specify [1], and extend it, you have undefined behavior when
you attempt to access anything but the first element.
Are we obliged to put the fData2 fied as the *last* element in
the Foo1?
Yes. Otherwise, how would the compiler (not knowing n) know how
to find the other elements.
Is there any better/ safer manner to implement the same
pattern?
Even in shared memory, I'd keep variable length arrays separate.
Perhaps using some sort of smart pointer which only stores the
offset from the beginning of shared memory, and uses a global
variable (in the non-shared memory of each process) to calculate
the real address.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #7
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...cess_container

Regards, Frank
Jun 27 '08 #8
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...cess_container
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.

--
Ian Collins.
Jun 27 '08 #9
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).

Regards, Frank
Jun 27 '08 #10

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

Similar topics

17
1926
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
3175
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
2616
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
8079
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
2033
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
4596
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
3343
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
9462
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
9287
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
10046
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
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...
0
9722
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...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6542
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
5155
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...
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.