Re: Copying a struct to a larger struct?



On Fri, 29 Jul 2005 00:56:03 +0400, Alexei A. Frounze wrote:

> "Barry Schwarz" <schwarzb@xxxxxxxxx> wrote in message
> news:g7uge1pv97iu2j9faooaguv3234gafc8ok@xxxxxxxxxx
>> On 27 Jul 2005 12:32:08 -0700, "hermes_917@xxxxxxxxx"
> ...
>> There is no guarantee that the padding in the common part of the two
>> structs is the same.
>
> If my memory serves me, the standard gurantees that.

Perhaps you aren't using error correction on your memory. :)

Barry is right, the standard doesn't guarantee it. The alignment of
members within a struct is "implementation-defined" (I can't imagine why
in practice a compiler would not pad them identically, but it isn't
prohibited from padding differently).

The only guarantees about member positions in structs are that no hole
will occur at the beginning and that members will occupy increasing
storage addresses.

Padding also may be added at the end to ensure correct alignment for
arrays of the struct (but this is again at the implementation's discretion
- i.e. not of predictable / guaranteed / repeatable size).

> But, I'd prefer to turn
> that common part into a type used inside both structs. memcpy() will be able
> to do bad things silently...

memcpy() may do bad things if the common part is not padded identically,
as well as if the smaller struct has padding at the end in space at which
the larger struct has a member.

You're right that your preferred solution is safe though.

.



Relevant Pages

  • Re: padding?
    ... Padding may change randomly or never at all, and you can blot over it ... struct s { ... type to access the "hidden" members. ... There is one case where unnamed bit-fields are ...
    (comp.lang.c)
  • Re: tokens concat
    ... I think that if the struct contains only members of type int, ... Do you mean that excluding padding; in this case st should place all ... Yes, if all members were of type int, there would be no padding ... tricks to ensure that the code will compile only when this test is ...
    (comp.lang.c)
  • Re: tokens concat
    ... I think that if the struct contains only members of type int, ... Do you mean that excluding padding; in this case st should place all ... Yes, if all members were of type int, there would be no padding ... Since a compiler may add padding after the last element of a ...
    (comp.lang.c)
  • Re: How come C allow structure members to be addressed like an array ?
    ... but I'm open to correction!) ... i.e. whether there are any padding bytes in the struct that would ... And it seems on anything but a DS9K it is save to assume that no padding ... bytes are between the members, but it is very well possible ...
    (comp.lang.c)
  • Re: Curious Question about Struct Padding
    ... C89-99 standard that prohibits the padding bytes within or after a ... struct from being suitable storage for another object. ... least in the C99 standard), although it would not be the first time I ... Using the member ...
    (comp.std.c)