Re: Practical packing for structs of bytes



On Mon, 20 Sep 2010, Michael Henry wrote:

On Sep 17, 1:19 pm, "Ersek, Laszlo" <la...@xxxxxxxxxxxxxx> wrote:
On Fri, 17 Sep 2010, Michael Henry wrote:
   uint8_t *buf = ...buffer of raw protocol bytes...;

   ProtocolHeader *p = (ProtocolHeader *) buf;

I wanted to add before: dependent on the placeholder code that provides the initializer to "buf", this may still invoke undefined behavior, in-effect implementation-dependent "structure packing" notwithstanding.

Is the undefined behavior due to alignment restrictions on the struct,

Yes, that's what I had in mind.

Cheers,
lacos

Relevant Pages

  • Re: reassigning pointers
    ... char *buf; ... operators made a passing comment that you couldn't reassign references ...
    (comp.lang.c)
  • Re: reassigning pointers
    ... char *buf; ... operators made a passing comment that you couldn't reassign references ...
    (comp.lang.c)
  • Re: reassigning pointers
    ... int main ... char *buf; ...
    (comp.lang.c)
  • Re: How to make (ptr + len) > lim safe?
    ... William Ahern wrote: ... to make sure I don't write past the end (again assuming blim is valid). ... So what if buf was NULL? ... If you write to an invalid address that's undefined behavior. ...
    (comp.lang.c)
  • Re: Line/word wrap program.
    ... On 2004-08-20, Barry Schwarz wrote: ... > invoke undefined behavior. ... lclint is a bit laconic at ...
    (comp.lang.c)