Contestcen@aol.com wrote:
Peter's original posting on this subject asked if there was a way to store character strings of different lengths without wasting space for the short strings. Frank Heckenbach suggested that Peter store each string using the New procedure, so each string would get a space its own size. I said that the Pascal Macro Compiler could store the strings without any gaps or wasted space. However, this works only if the strings are known at compile time.
I also pointed out that using New does not assure that there would be no gaps because New does not always allocate consecutive adjacent blocks of storage. In the storage-allocation system that I use most often, storage is allocated in units of 16 bytes, because free storage is kept in a tree structure with each element containing a length, an up pointer and two down pointers.
That's one method. However, AFAIK, modern memory managers use something more elaborate, such as keeping free blocks of different sizes in separate lists/trees/pages.
So if you allocated a separate block for each character string there could be a lot of wasted space.
OTOH, strings must be aligned at least to `Integer' size. (Accessing the Capacity and Length fields requires alignment on some platforms and is at least more efficient on others; accessing the characters can also be more efficient when aligned.) Does your preprocessor take care of this?
(But again, I'm quite sure that Peter's question referred to runtime storage, so that's all kind of beside the point.)
Conclusion: If the strings are known at compile time, the most efficient way to store them without gaps is to use the Pascal Macro Compiler. If the strings are not known until run time, the most efficient way is to allocate a large block of storage, and move the strings in end-to-end, using a separate array to hold either pointers or indices into the large block.
Not necessarily. There are various data structures such as hashes, lists and trees for a reaons. Each have their particular advantages and disadvantages. Which one is best, depends on the application and is not always easy to decide. Claiming that any particular method is "the most efficient" without qualification doesn't give your statements much credibility, sorry.
PS: Can you please turn off HTML mail. At least you got the quoting right this time. :-)
Frank