Topic: memcpy on base class objects


Author: James Kanze <kanze@alex.gabi-soft.de>
Date: Wed, 22 May 2002 20:10:15 GMT
Raw View
David Schwartz <davids@webmaster.com> writes:

|>  John the newbie wrote:

|>  > it has been said me on c.l.c++.m that if I write

|>  > struct A {};

|>  > class B : public A {};

|>  > than each time I convert a B* to A* I have a "real" POD struct.

|>  > Now I wonder, is this legal?

|>  > B b;
|>  > A pod;

|>  > memcpy(&a, &b, sizeof(A));

|>   Clearly not, since &b is not a pointer to a real POD struct.

A more interesting question would be whether the following is legal:

    memcpy( static_cast< A const* >( &b ), &a, sizeof( A ) );

In this case, both pointers are pointers to "real" POD structs.  On
the other hand, given the definition of B, the compiler can "optimize"
the size of the base class -- if it does so, this code could overwrite
part of the B part of b.

--=20
James Kanze                                mailto:kanze@gabi-soft.de
Conseils en informatique orient=E9e objet/
                    Beratung in objektorientierter Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49(0)179 2607481

---
[ 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: "Heinz Ozwirk" <wansor42@gmx.de>
Date: Mon, 13 May 2002 17:46:15 GMT
Raw View
"John the newbie" <whatiscpp@yahoo.com> wrote in message
news:102a8848.0205120753.4e521552@posting.google.com...
> Hi everybody,
>
> it has been said me on c.l.c++.m that if I write
>
> struct A {};
>
> class B : public A {};
>
> than each time I convert a B* to A* I have a "real" POD struct.
>
> Now I wonder, is this legal?
>
>
> B b;
> A pod;
>
> memcpy(&a, &b, sizeof(A));

No (not only because a is undefined). Calling memcpy, you do not convert a
B* (&b) to an A*. You convert it to void*. If you want to copy the A-part of
B to a "real" A object you'd better use A's assignment operator:

    a = b;

If you really want to use memcpy, you must use an explicit cast:

    memcpy(&a, (A*)&b, sizeof(A));

That might work (but don't blame me if it doesn't). Perhaps that yould copy
some bytes from the B-part of b into padding bytes of a, but that shouldn't
matter. And don't try to copy an A into the A-part of a B:

    /* DON'T: */ memcpy((A*)&b, &a, sizeof(A));

This may destroy vital data of b.


Regards
    Heinz


---
[ 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: whatiscpp@yahoo.com (John the newbie)
Date: Sun, 12 May 2002 17:32:09 GMT
Raw View
Hi everybody,

it has been said me on c.l.c++.m that if I write

struct A {};

class B : public A {};

than each time I convert a B* to A* I have a "real" POD struct.

Now I wonder, is this legal?


B b;
A pod;

memcpy(&a, &b, sizeof(A));


Thanks

---
[ 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: James Dennett <jdennett@acm.org>
Date: Sun, 12 May 2002 17:45:59 GMT
Raw View
John the newbie wrote:
> Hi everybody,
>
> it has been said me on c.l.c++.m that if I write
>
> struct A {};
>
> class B : public A {};
>
> than each time I convert a B* to A* I have a "real" POD struct.
>
> Now I wonder, is this legal?
>
>
> B b;
> A pod;
>
> memcpy(&a, &b, sizeof(A));

No, it is not.  In particular, in the case you've written,
the A sub-object of 'b' can be allocated no space at all
using the "empty base optimization", but sizeof(A) must
of course be at least 1, so you'll be overwriting at least
one byte of the non-POD B, which might do anything.  In
general I *think* that a derived class can use padding
space in base sub-objects to store data.

--
James Dennett <jdennett@acm.org>

---
[ 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                       ]