Variable-length messages.
Dave Jones
djones at megatest.UUCP
Sat Oct 22 09:18:41 AEST 1988
>From article <LJZ.88Oct20131114%fxgrp.fx.com at ames.arc.nasa.gov>, by ljz%fxgrp.fx.com at ames.arc.nasa.gov (Lloyd Zusman):
> We have a difference of opinion at our site as to what is the most
> desirable way to handle variable-length messages in C.
...
> One group of us here says it's OK to handle this case as follows:
>
> struct message {
> int msgType; /* msg type code */
> int msgLength; /* length of msg body */
> char msgBody[1]; /* variable length msg body */
> };
>
This looks unsafe to me. That one-char-long "msgBody" can be
packed into the structure at any of a number of different places,
depending on the compiler's alignment strategy.
The "msgBody" is not a one-char-long arrary. In general, it's not nice
to fool Mother Nature.
One thing you might consider is using Sun's public domain "XDR".
I think you can get it from anonymous ftp somewhere around the net.
It's got some defects, but it *is* already written, and it *is*
free. Check it out.
More information about the Comp.lang.c
mailing list