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

Discussion in 'C++' started by FFMG, Feb 22, 2008.

  1. FFMG

    FFMG Guest

    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.
     
    FFMG, Feb 22, 2008
    #1
    1. Advertising

  2. 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::eek:perator=(structA);

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


    Marcel
     
    Marcel Müller, Feb 22, 2008
    #2
    1. Advertising

  3. 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;
    }
     
    Gianni Mariani, Feb 22, 2008
    #3
  4. FFMG

    James Kanze Guest

    On Feb 22, 11:42 am, Gianni Mariani <> 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
     
    James Kanze, Feb 22, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    1,235
  2. Leo Nunez
    Replies:
    3
    Views:
    1,231
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
  5. David
    Replies:
    3
    Views:
    403
    Grizlyk
    Jan 29, 2008
Loading...

Share This Page