Xref: utzoo comp.arch:14205 comp.lang.c:26262 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!unido!mikros!mwtech!martin From: martin@mwtech.UUCP (Martin Weitzel) Newsgroups: comp.arch,comp.lang.c Subject: Re: RISC Machine Data Structure Word Alignment Problems? Message-ID: <662@mwtech.UUCP> Date: 23 Feb 90 22:47:19 GMT References: <645@mwtech.UUCP) <12118@goofy.megatest.UUCP> Reply-To: martin@mwtech.UUCP (Martin Weitzel) Organization: MIKROS Systemware, Darmstadt/W-Germany Lines: 61 In article <12118@goofy.megatest.UUCP> djones@megatest.UUCP (Dave Jones) writes: }From article <645@mwtech.UUCP), by me (Martin Weitzel): }) There were some recent postings, that pointed out/complained about }) 'holes' in C-struct definitions. }... }) }) My advice in this situation is, to change this code as follows: }) }) char buffer[ }) 2 /* length of message */ }) + 1 /* several flag bits }) + 1 /* type of message */ }) + 4 /* checksum */ }) + 100 /* arbitrary message */ }) ]; }) }) #define m_length(b) (*((short *) (char *)(b) + 0)) }) #define m_flags(b) (*((unsigned char *)(char *)(b) + 2)) }) #define m_type(b) (*((char *) (char *)(b) + 3)) }) #define m_checksum(b) (*((unsigned long *)(char *)(b) + 4)) }) #define m_bytes(b) ( (char *)(b) + 8 ) }) } }There's probably going to be a flurry of replies telling you why }this will not work in the general case. } }These casts from char* to this-or-that* are not going to work }unless the data just happen to be properly aligned for whatever }processor you happen to be using. I'm well aware that allignment restrictions may invalidate certain casts from one pointer type to another, but you must see my proposual in the context of the original questions: The posters generally complained, that they were not able to overlay certain byte patterns in memory, because the C-struct they defined for that purpose contained holes (introduced by the compiler). The question, by which hard- or software the byte patterns were produced, was never mentioned in these postings, but because the posters seemed to be sure, that (only) the holes in the structures caused the problems, the parts must have been allready properly aligned If the parts of the byte patterns were not properly aligned, also struct-s *without* holes could not have been used for this purpose(%). So my proposual is not worse than a struct, but sometimes helps to get (better) control of which memory locations are accessed, than struct-s can provide. If it is only necessary to *read-access* the bytes in question, the approach described later in my original posting for getting 'wrong' byte order 'right', may also be used in case of not properly aligned short-s, int-s or long-s. (%) If a compiler, which supports an option to pack structures, does this *always tightly*, even on systems with specific alignment requirements for short-s, int-s and long-s, it may emit code to acces the LSB/MSB idividual and combine them in a register, but this would be such an extreme performance penalty, that I guess such compilers are rare. -- Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83