Re: structure alignment

From: Jack Masters (jcfmasters_at_yahoo.com)
Date: 09/08/05


Date: Thu, 08 Sep 2005 11:24:38 +0200

Sagaert Johan wrote:
> "David Schwartz" <davids@webmaster.com> wrote in message
> news:dfohh5$vnr$1@nntp.webmaster.com...
>
>>"Sagaert Johan" <sagaert.j AT belgacom.net> wrote in message
>>news:431f3ec8$0$28236$ba620e4c@news.skynet.be...
>>
>>
>>>i need it becorse i receive an arry of bytes from a serial comm routine
>>>that
>>>is copied into the structure (enum )
>>
>> When you copy it into the structure, put everything where it goes. You
>>*cannot* rely on a structure having a particular representation in memory
>
> at
>
>>the byte level.
>>
>> DS
>>
>>
>
> When i learned C i came across a section that showed the layout in memory
> follows the layout of the structure, the only thing i came across si that
> byte ordering for int is different on some platforms.
> So far i never came into problems using this technique.
>
> struct s_remote
> {
> char name[10];
> char prefix[20];
> int flags;
> } ;
>
> union u_remote
> {
> unsigned char B[sizeof s_remote];//as bytes
> struct s_remote S; //as structure
> } ;
>
> union u_remote myvar;
>
> //receive from a remote device through comport
> while (n< sizeof myvar)
> {
> myvar.B[n]= received_byte();
> n++;
> }
>
> // using my data
> printf(" data=%s",myvar.S.prefix);
>
>
>

This is how we did all the comms and IPC work under CCPM, and there is
nothing wrong with it on 99% of platform/compiler combinations. Provided
you are using the right compiler options, like no aggressive
optimizations, and byte-aligned struct members of course. However, on
the remaining 1% it might bomb on you (e.g. your structure defines a
32-bit int that is not dword-aligned, the compiler generates a 32-bit
fetch, and the CPU can't handle that).

This is why the current generation gurus agitates against this concept.
If you are writing something for a single platform, and for *in-house
use only*, fine. If you want to release the code into the wild, and make
it anywhere near portable, don't use this.

J.



Relevant Pages

  • Re: structure alignment
    ... > follows the layout of the structure, ... > byte ordering for int is different on some platforms. ... properly unpack each element. ...
    (comp.os.linux.development.apps)
  • Re: TR 24731 approved
    ... they're tied to a specific type, long int. ... long int was supposed to be the largest type in C90 (well, ... On such platforms it would have been meaningful to have a 64 bits long ... Implementations should probably have added their ...
    (comp.std.c)
  • Re: Fast / Slow Line Layout
    ... What I mean is this, on some routes the layout seems to be Up Slow, Up fast, ... had through roads at the stations (which ... In the case of the Met, if Chiltern ever got their platforms at ... of West Hampstead would be a good idea. ...
    (uk.railway)
  • Re: a question regarding
    ... I would very much like to see the ABI change happen, and the first step has been done already as part of the MAC work. ... By changing the size of a field in a data structure, you may change the layout of the structure, and hence the offset of other fields. ... On i386, the change from int to size_t doesn't modify the ABI, as both int and size_t are 32-bit. ... This is why DES and I have been discussing this change as requiring kernel compatibility code, which would provide new system calls working with the new layout, and retain old system calls working with the old layout. ...
    (freebsd-hackers)
  • Re: Thou shalt have no other gods before the ANSI C standard
    ... platforms, on DJB's list (other than 32-bit int)? ... programmers doing the porting to wacky platforms to deal with these ... this is the reality. ...
    (sci.crypt)

Loading