RISC Machine Data Structure Word Alignment Problems?

Dave Jones djones at megatest.UUCP
Fri Feb 23 13:01:58 AEST 1990


>From article <645 at mwtech.UUCP), by martin at mwtech.UUCP (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.



More information about the Comp.lang.c mailing list