Re: Question on use of "placement" new
letz@grame.fr wrote in news:dd732c4c-3cd7-4319-ae60-be2cb2a487a6
@d77g2000hsb.googlegroups.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::operator 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<Barr*>( static_cast<void*>(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