Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!cornell!batcomputer!itsgw!steinmetz!uunet!ingr!jones From: jones@ingr.UUCP (Mark Jones) Newsgroups: comp.lang.c Subject: Re: Variable-length messages. Message-ID: <2751@ingr.UUCP> Date: 27 Oct 88 13:16:06 GMT References: <306@lakart.UUCP> Organization: Intergraph Corp. Huntsville, Al Lines: 28 [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