[comp.unix.wizards] word alignment for structures in UNIX

mikey@quiche.cs.mcgill.ca (Michael GALLOP) (09/25/90)

Sorry, my rn is busted...I gotta do this by hand.
In article 20631 <4046@quiche.cs.mcgill.ca > (adr@quiche.cs.mcgill.ca)
(Alain Durand) writes:
>
>Hello,
>
>       I've troubles with word alignments on different unix machine:
>sizeof(struct foo) doesn't return the same thing on a sun3, sun386i
>and sparc. Is there an option to set at compilation time or somewere
>in the kernel to fix that?
>thanks for any answer, please reply by E-Mail at:
>
>adr@quiche.cs.mcgill.ca





Please note: The poster is only a grad student at Mcgill and does not reflect 
the undergrad education :- ) 


Well Alain. There are several different factors. First off when dealing with 
any two machines, your request will likely return different values. 


One of the reasons-perhaps the main one- is the different architectures in 
the CPUs of the machines. From what I remember 80X86 swaps the bytes around 
from the way the SPARC deals with them 


More importantly is the difference in word size. An 80386 is a 32 bit machine 
-in some modes. In fact all of these are 32 bit machines. However the 
register addressing is different on each one. The Sparc uses circular 
addressing, whereas the i386 uses a form of virtual addressing. 

Further, an important factor is the overhead of your C compiler, your OS. 
These _MIGHT_ ( probably won't) affect the result returned.... 
This, coupled with the different ways that each OS allocates memory 
will result in your observations. 


Why, though are you asking ? You can't magic number a (Not can't- shouldn't) 
malloc. So you should 
if ( ( malloc ( sizeof ( foo ) ) ==NULL ) 
or whatever is needed. 

As for patching it in the kernel, I couldn't possibly see why, or how, you'd 
do that. By hard wiring a value like that in the kernel, you cause havoc in 
the rest of the OS. Especially in a 386. If you were to do that on a 386 in 
386 protected mode, or, worse in virtual 8086 mode, the page frame would 
cause a memory fault- I should know, I spent many hours chasing my mistake 
on that one I tried to align with a magic number :- ( -and dump the machine.