Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!bu-cs!purdue!decwrl!megatest!djones From: djones@megatest.UUCP (Dave Jones) Newsgroups: comp.lang.c Subject: Re: Variable-length messages. Message-ID: <919@goofy.megatest.UUCP> Date: 21 Oct 88 23:18:41 GMT References: Organization: Megatest Corporation, San Jose, Ca Lines: 29 From article , by ljz%fxgrp.fx.com@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.