Re: Alignment of variables in structs
- From: SM Ryan <wyrmwif@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 30 Nov 2007 02:44:47 -0000
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.
.
- References:
- Alignment of variables in structs
- From: Rick
- Alignment of variables in structs
- Prev by Date: Re: pointer as argument and parameter
- Next by Date: Re: New language features?
- Previous by thread: Re: Alignment of variables in structs
- Next by thread: Get the Compile-time Environment Variables ?
- Index(es):
Relevant Pages
|