Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #15767

Re: struct alignment

From "Scott Fluhrer" <sfluhrer@ix.netcom.com>
Newsgroups comp.lang.c
References <jekkil$u7v$1@speranza.aioe.org>
Subject Re: struct alignment
Message-ID <1326319096.33357@rcdn-nntpcache-3> (permalink)
Organization cisco.com
Date 2012-01-11 16:58 -0500

Show all headers | View raw


"Mark" <mark_cruzNOTFORSPAM@hotmail.com> wrote in message 
news:jekkil$u7v$1@speranza.aioe.org...
> Hello:
>
> #define MAX_SIZE 8
>
> enum my_enum
> {
>    VAL1 = 0;
>    VAL2,
>    VAL3,
>    VAL4,
>    VAL_MAX
> };
>
> struct my_struct
> {
>  enum my_enum e;
>  int  w[MAX_SIZE];
> };
>
> Can a layout in such structure cause alignment issues on a target 
> platform?

Nope; it is the compiler's job to make sure that structure elements are 
aligned properly (whatever "properly" means for the target platform).

> I understand it much depends on a platform,

Well, yes, the definition of "properly" varies between platforms, and in 
addition, compilers sometimes add more padding than strictly required (for 
example, because they expect to get a performance benefit out of it)

> but generally C compiler is allowed to do padding of structures,

C compilers are always allowed to add padding to structures (except, of 
course, in front of the first element)

> so for example on 32 bit machine, where 'int' is 32 bit long:
>
> struct my_struct
> {
>    int w[MAX_SIZE];
> }
>
> is aligned (as fas as I understand) so compiler probably won't be doing 
> anything else with its layout, but adding 'enum my_enum' in the structure 
> can potentially get the structure un-aligned on such machine.

Nope; the compiler will add padding (if needed) between the structure 
members e and w.

> Should I be doing anything special to avoid this and should I be avoiding 
> it at all ?

If that structure definition is what you mean, there is absolutely no reason 
to avoid it.

-- 
poncho

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

struct alignment "Mark" <mark_cruzNOTFORSPAM@hotmail.com> - 2012-01-11 13:31 -0500
  Re: struct alignment Kleuske <kleuske@nowhere.net> - 2012-01-11 19:23 +0000
  Re: struct alignment "Scott Fluhrer" <sfluhrer@ix.netcom.com> - 2012-01-11 16:58 -0500
  Re: struct alignment Ralf Damaschke <rwspam@gmx.de> - 2012-01-11 22:08 +0000
  Re: struct alignment Keith Thompson <kst-u@mib.org> - 2012-01-11 14:42 -0800
    Re: struct alignment Dr Nick <3-nospam@temporary-address.org.uk> - 2012-01-12 06:56 +0000
      Re: struct alignment Ian Collins <ian-news@hotmail.com> - 2012-01-12 20:42 +1300
        Re: struct alignment Keith Thompson <kst-u@mib.org> - 2012-01-12 02:20 -0800
          Re: struct alignment Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-18 17:03 +0000
            Re: struct alignment Kaz Kylheku <kaz@kylheku.com> - 2012-01-18 17:21 +0000
              Re: struct alignment jgk@panix.com (Joe keane) - 2012-01-19 21:42 +0000
                Re: struct alignment blp@cs.stanford.edu (Ben Pfaff) - 2012-01-19 13:57 -0800
            Re: struct alignment Stephen Sprunk <stephen@sprunk.org> - 2012-01-18 11:57 -0600
              Re: struct alignment Keith Thompson <kst-u@mib.org> - 2012-01-18 14:28 -0800
            Re: struct alignment Robert Wessel <robertwessel2@yahoo.com> - 2012-01-18 13:10 -0600
              Re: struct alignment Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-19 11:34 +0000
                Re: struct alignment Robert Wessel <robertwessel2@yahoo.com> - 2012-01-19 14:50 -0600
                Re: struct alignment Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-21 11:49 +0000

csiph-web