Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > copy derived structure, is it legal c++ code?

Reply
Thread Tools

copy derived structure, is it legal c++ code?

 
 
FFMG
Guest
Posts: n/a
 
      02-22-2008
Hi,

I am trying to copy some variables from a struct to a derived struct.
If I have something like,

....
struct STRUCTA
{
int numA;
long numB;
float numC;
}

struct STRUCTB public STRUCTA
{
int numD;
}
....

STRUCTA structA = {0,1,2};
STRUCTB structB;
memcpy( &structB, &structA, sizeof( STRUCTA) );

....
//
Is the above code legal to copy the structure from structA to structB

Thanks

F.
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      02-22-2008
FFMG schrieb:
> I am trying to copy some variables from a struct to a derived struct.
> If I have something like,
> ...
> struct STRUCTA
> {
> int numA;
> long numB;
> float numC;
> }
>
> struct STRUCTB public STRUCTA
> {
> int numD;
> }

[There are many syntactical errors up to now.]
> ...
>
> STRUCTA structA = {0,1,2};
> STRUCTB structB;
> memcpy( &structB, &structA, sizeof( STRUCTA) );
> ...
> //
> Is the above code legal to copy the structure from structA to structB


It is not valid. It is undefined bahaviour.
STRUCTB is no longer a POD type because of the inheritance.

However, as long as neither STRUCTA or STRUCTB has virtual Methods or
things like that it usually works. But think what happens if STRUCTB is
defined as:

struct STRUCTB : public STRUCTC, public STRUCTA
{
int numD;
}

Your code will most likely fail badly.

And as far as I know there is no guarantee that the A part of B is in
the front of B. This is implementation defined.


But there is no need for the memcpy in your case at all since C++
supports slicing. B has also the properties of A, so you can assign the
A slice independantly. But this is no implicit conversion.

You must either explicitely address the A part by doing a cast

static_cast<STRUCTA&>structB = structA;

or you must call the assignment operator of STRUCTA explicitely

structB.STRUCTA:perator=(structA);

I would prefer the first since I am unsure wether the second one is a
language extension of gcc.


Marcel
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      02-22-2008
FFMG wrote:
> Hi,
>
> I am trying to copy some variables from a struct to a derived struct.
> If I have something like,
>
> ...
> struct STRUCTA
> {
> int numA;
> long numB;
> float numC;
> }
>
> struct STRUCTB public STRUCTA
> {
> int numD;
> }
> ...
>
> STRUCTA structA = {0,1,2};
> STRUCTB structB;
> memcpy( &structB, &structA, sizeof( STRUCTA) );
>
> ...
> //
> Is the above code legal to copy the structure from structA to structB


I'm not sure, however, this is:

struct STRUCTA
{
int numA;
long numB;
float numC;
};

struct STRUCTB : STRUCTA
{
int numD;
};

int main()
{
STRUCTA structA = {0,1,2};
STRUCTB structB;
STRUCTA & rstructB = structB;
rstructB = structA;
}

You could probably make a template that did that for you. e.g.

template <typename T1, typename T2>
T2 & assign( T1 & v1, const T2 & v2 )
{
T2 & rv1 = v1;
return rv1 = v2;
}
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      02-22-2008
On Feb 22, 11:42 am, Gianni Mariani <gi4nos...@mariani.ws> wrote:
> FFMG wrote:


> > I am trying to copy some variables from a struct to a derived struct.
> > If I have something like,


> > ...
> > struct STRUCTA
> > {
> > int numA;
> > long numB;
> > float numC;
> > }


> > struct STRUCTB public STRUCTA
> > {
> > int numD;
> > }
> > ...


> > STRUCTA structA = {0,1,2};
> > STRUCTB structB;
> > memcpy( &structB, &structA, sizeof( STRUCTA) );


> > ...
> > //
> > Is the above code legal to copy the structure from structA to structB


> I'm not sure,


It's undefined behavior for two reasons. First, as Marcel
Müller points out, there is no guarantee that the address of
structA (converted to a void*) is the same as the address of the
STRUCTB subclass. Secondly, because there is no requirement
that the compiler allocate all of the memory of an object when
it is a base class. (In practice, I don't think any compilers
apply this optimization other than for an empty class,
allocating zero bytes, although sizeof( Class ) returns 1. But
the standard allows it any time a type is used as a base class.)

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) developereo@hotmail.com C++ 4 05-23-2007 09:32 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57