Re: portability question



On Tue, 2003-12-16 at 11:55, John Cupitt wrote:
> One of the other large assumptions that glib makes is about struct alignment. 
> It assumes that if you have
> 
> struct fred {
> 	int a;
> };
> 
> struct jim {
> 	struct fred foo;
> 	int b;
> };
> 
> You can do 
> 
> 	struct jim *jim = ...;
> 
> 	((struct fred *) jim)->a = 12;
> 
> ie. struct members don't move about strangely. 
> 
> Like some of the other assumptions you talked about, the justification is that 
> it works for all platforms people care about, and makes code much simpler.

Actually, this one is guaranteed by ANSI C - 

6.7.2.1 13 "Within a structure object, the non-bit-field members and
the units in which bit-fields reside have addresses that increase in the
order in which they are declared. A pointer to a structure object,
suitably converted, points to its initial member (or if that member is a
bit-field, then to the unit in which it resides), and vice versa. There
may be unnamed padding within a structure object, but not at its
beginning."

Regards,
						Owen






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]