There are many ways to handle string data, but the two most often seen are;
1. C style. Each string is preallocated a block of memory. This limits the length of the string and causes problems if you try to write more characters than that maximum. The actual end of the current string is marked by a null (0) byte, and a common C programmer’s error is forgetting to allow an extra byte in the string declaration to hold that null. This is simple to compile but it invites you to shoot yourself in the foot. A C programmer should explicitly test the length of a string before every statement that might increase the length of the string. Failure to do this is what allows the common “buffer overrun’ hack that plagues so many C-implemented internet applications.
2. Microsoft BASIC style. Each string has a fixed length string descriptor containing the current length of the string and the address of the first byte of the value of the string, which is kept in a heap called “string space.” A string whose current value is null has zero length. There is also a “back pointer”, stored in string space before the first character of the string. This is used by a “garbage collector” routine that moves strings around in string space to reclaim space that was used by strings that have been abandoned. This approach is obviously harder to implements but it allows any string to grow until it fills all available string space. This also prevents buffer overruns; it the runtime system can’ t find enough space to complete a requested assignment it raises an error flag that halts the program .
Heap allocation also makes string arrays easy. An array of strings is just an array of fixed-length string descriptors. In QuickBasic and later Microsoft BASIC a descriptor is 32 bits long 16 for the length and a 16-bit pointer into string space.
Comments
Post a Comment