Variable-length messages.

Mark Jones jones at ingr.UUCP
Thu Oct 27 23:16:06 AEST 1988


[stuff deleted]

> 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.
	You can create an array of these things.
	Lint won't gripe.
	The compiler won't gripe.

> Disadvantages:
> 
> 	Requires an extra malloc(3) call every time you want to do anything.

Not necessarily, you could malloc a big chunk of memory, fgets into it,
saving pointers to the start of each message.  This is a good thing if
you are on a machine of limited memory.  Allocate an array of message
structures, and allocate a chunk for all the text.  MSDOS allocates
memory with a 16 byte minimum, this scheme can save a lot of memory.

Mark Jones



More information about the Comp.lang.c mailing list