Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mcvax!unido!iaoobelix!wagner From: wagner@iaoobelix.UUCP Newsgroups: comp.lang.c Subject: Re: Byte vs. Word Alignment - (nf) Message-ID: <6700007@iaoobelix.UUCP> Date: Sat, 18-Apr-87 06:03:00 EST Article-I.D.: iaoobeli.6700007 Posted: Sat Apr 18 06:03:00 1987 Date-Received: Tue, 21-Apr-87 02:18:59 EST Lines: 39 Nf-ID: #N:iaoobelix:6700007:000:1605 Nf-From: iaoobelix!wagner Apr 18 12:03:00 1987 Byte vs. word alignment is a problem I have encountered recently when I tried to setup a simple server/client connection using sockets. The problem was that you get very strange results on a VAX/uVAX/Sun if you print out sizeof(struct foo) where struct foo is: struct foo { char frob_1; int frob_2; }; I intended to send something like the above structure as a packet over the net. You know what happened? The VAX/uVAX produces *8* as sizeof struct foo, whereas the Sun program yields *6*. The client program, run on the VAX sent a packet of size 8, and the daemon on the Sun thought the packet size was 6. Yet, things get even worse if you use something like struct packet { char packet_type; struct foo packet_data; } Aarrgh! You get *8* on a Sun and *12* on a VAX/uVAX. The whole structure is not being packed together into eight bytes (with the two chars in the first word) but each char is placed in a 4-byte (four-byte!) word! BTW, the same holds for shorts (2 bytes). Maybe there should be a compiler switch -dont_align forcing the compiler to take things literally, i.e. to leave these structs unchanged even if their size is an odd number of bytes. The structures themselves could be word aligned *in memory*, however, but the internal structure would be as the programmer defined it. Note: If you think of something like `unsigned frob: 8;' to get the desired result... No chance! You will get exactly the same sizes for the structures on both Sun and VAX/uVAX. Juergen Wagner, (USENET) ...seismo!unido!iaoobel!wagner ("Gandalf") Fraunhofer Institute IAO, Stuttgart