Variable-length messages.

David Goodenough dg at lakart.UUCP
Tue Oct 25 02:34:48 AEST 1988


Lloyd sez:
> We have a difference of opinion at our site as to what is the most
> desirable way to handle variable-length messages in C.  This is
> basically a difference in philosophies.  I'm showing you folks on the
> net the two opposing approaches in question here, and I would like to
> find out which of these each of you prefers and why.
> 
> 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 */
> 	};
> 
> Another group says that since the 'msgBody[1]' field really isn't one
> byte long, its use is misleading and would confuse programmers and
> debugging software, not to mention the fact that they feel it isn't
> "pure". .....

As an alternative to the above, how useable would the following be:

 	struct message {
 		int msgType;		/* msg type code */
 		int msgLength;		/* length of msg body */
 		char *msgBody;		/* variable length msg body */
 	};

Advantages:

	Is portable, and doesn't fry the mind of dbx and friends.

Disadvantages:

	Requires an extra malloc(3) call every time you want to do anything.
-- 
	dg at lakart.UUCP - David Goodenough		+---+
							| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp at harvard.harvard.edu	  	  +---+



More information about the Comp.lang.c mailing list