Re: Copying a struct to a larger struct?
- From: Netocrat <netocrat@xxxxxxxxxxx>
- Date: Fri, 29 Jul 2005 11:40:43 +1000
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.
.
- Follow-Ups:
- Re: Copying a struct to a larger struct?
- From: Keith Thompson
- Re: Copying a struct to a larger struct?
- References:
- Copying a struct to a larger struct?
- From: hermes_917@xxxxxxxxx
- Re: Copying a struct to a larger struct?
- From: Barry Schwarz
- Re: Copying a struct to a larger struct?
- From: Alexei A. Frounze
- Copying a struct to a larger struct?
- Prev by Date: Re: Macro calling kernel function
- Next by Date: Re: Macro calling kernel function
- Previous by thread: Re: Copying a struct to a larger struct?
- Next by thread: Re: Copying a struct to a larger struct?
- Index(es):
Relevant Pages
|