Topic: is C++ implementation allowed to store object in non-continuous


Author: jdennett@acm.org (James Dennett)
Date: Fri, 11 Jul 2003 01:36:14 +0000 (UTC)
Raw View
Victor Bazarov wrote:
> "Bo-Staffan Lankinen" <bo-staffan.lankinen@gbg.bonet.se> wrote...
>
>>>What does it matter whether the bytes are contiguous or spread around?
>>>If you want to copy the object using memcpy or memmove you may only
>>>do that with POD.  Any other operation does not require the object to
>>>be in contiguous memory, does it?
>>
>>I can matter when constructing with placement new.
>
>
> I am not sure what you mean.  How would that matter?

struct T { ... };
unsigned char buffer[sizeof(T)];
new (static_cast<void*>(p)) T;

This must construct a T object in the buffer.  It has
to assume that the buffer is contiguous, and use it.

> Every
> 'new' operator works with memory address returned to it by
> the allocation function (3.7.3.1), placement new is not in
> any way different, it just passes extra arguments to that
> allocation function.

The allocation function in this case returns the address
it's given, and then an object is constructed at that
address.

> It's the job of the allocation function
> to recognise that the memory is split into pieces and not to
> use it, I suppose.

No, it's the job of the allocation function to return a
pointer to memory suitable for construction of an object.
The standard overload of ::operator new used by the
regular form of placement new simply returns the "extra"
argument it is given.

-- James.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: falk.tannhauser@crf.canon.fr (=?ISO-8859-1?Q?Falk_Tannh=E4user?=)
Date: Tue, 8 Jul 2003 01:36:13 +0000 (UTC)
Raw View
Mirek Fidler wrote:
> There is some indication that C++ implementation is allowed to store
> base class suboject of derived class in different chunk of memory. Is
> that true ? What paragraphs of standard deal with this issue ? (closest
> I have found is 3.9/5, but it does not seems to be definitive).
Think about multiple virtual inheritance!
>
> If eventually it is (personally, I do not think so), how is e.g.
> placement operator new expected to work ?
_____________________________________
#include <new>
struct A {};

struct B1 : public virtual A {};
struct B2 : public virtual A {};

struct C : public B1, public B2 {};

unsigned char mem_for_B1[sizeof(B1)];
unsigned char mem_for_C[sizeof(C)];

int main()
{
   // hoping the unsigned char arrays are well aligned...
   B1* b1 = new(mem_for_B1) B1;
   C* c   = new(mem_for_C) C;
   return 0;
}
_____________________________________
I don't think there is something special concerning placement new
since you can use it only to construct ***complete objects***
which always occupy a continuous memory zone (even if the B1 or
B1 subobjects within a C object doesn't).

Falk

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]