Re: Alignment of variables in structs



Rick <reply.in.newsgroup@xxxxxxx> wrote:

# Are there any rules that specify whether compilers are required to
# align variables on any specific boundaries?

The struct is large enough to hold all fields, and every
field without a bit width is addressable (you can do
&(s.f)). Beyond that, ANSI C doesn't give you a lot of
guarentees.

Individual compilers may give you tighter guarentees
but then you're tied to those compilers.

You pays your money and you takes your chances.

# I tried all of this on gcc on cygwin on Windows XP and got
# size_of_struct = 8, size_of_st_x = 8, and size_of_st_array = 40,
# rather than what one might expect at first glance, size_of_struct = 5,
# size_of_st_x = 5, and size_of_st_array = 25. Am I guaranteed to get
# that on all platforms?

You're guarenteed sizeof(struct st_x)>=sizeof(int)+sizeof(char)
by ANSI C. gcc has options to pack with no alignment spacing; that
will get you a size of 5, even if it slows down the loads and stores.
However that is gcc specific.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Leave it to the Catholics to destroy existence.
.



Relevant Pages

  • Re: Which PIC18 C Compiler?
    ... Walter gave a good answer regarding recursion in 8051 compilers, totally unrelated to gcc and/or sdcc (since there is no gcc port for the 8051). ... But in my experience on ColdFire's and PPC's, it has generated similar to or better than the few commercial compilers I have seen. ...
    (comp.arch.embedded)
  • Re: math.nroot [was Re: A brief question.]
    ... >>> Ah, but as I've said before, virtually all C compilers on 754 boxes ... >>> happy to use gcc, they all could have used the older gcc spellings ... > use glibc, they all could have used the older glibc spellings. ... for setting traps) interfaces, and I can't be arsed to go through old ...
    (comp.lang.python)
  • Re: math.nroot [was Re: A brief question.]
    ... >> happy to use gcc, they all could have used the older gcc spellings ... parings of C compilers and C runtime libraries are ... use glibc, they all could have used the older glibc spellings. ... trap doesn't actually fire until the _next_ fp operation ...
    (comp.lang.python)
  • Re: What is Linux built with? (or, what does this question mean?)
    ... Isn't the kernel/distro nearly always built w/ gcc? ... Now some of us actually have other compilers as well and these could be ... specific to provide cache and other optimizing that can't be generic. ... gnu compile in a way that it gives some registers over to the kernel as far ...
    (alt.os.linux.suse)
  • Re: g77 mex problem under win
    ... Don't have my matlab available, but as far as I can remember, ... Why do you insist on using gcc? ... You can buy good C compilers for a couple of hundred ... The same goes for me with the fortran compilers I have seen. ...
    (comp.soft-sys.matlab)