
Dim for strings ? (qbasic)
Quote:
>Hello Scott,
>> I'm confused about something. In Qbasic, dim a$(x, y) defines a 2
>dementional
>> array. How do you set how long each string is ? Surely they can't just set
>all
>> strings to 256 or something.
>Well, you've been tricked. It's not really creating a two-dimensional array
>of strings, it creates a two a two-dimensional array of *pointers to
>strings* ! This array is (AFAIK) *not* stored in String-space. The strings
>themselves however will be stored in (the dynamic) string-space.
Actually, DIM a$(x, y) creates an x-by-y array of "string
descriptors." Each descriptor is 4 bytes long, and contains the
length of the string (as a 2-byte integer; that's where the string
length limit of 32767 comes from) and a pointer to the first character
of the actual string is string space. The two bytes _before_ the
location pointed to contain a "back pointer" to the string descriptor.
This back pointer is used in garbage collection. Any dynamic string
thus has 6 bytes of overhead, two of which are in string space.
Most QB programmers don't appreciate the convenience of this scheme
until they learn (UGH!) C. The Microsoft dynamic string scheme is
usually well worth the extra memory. It may be worth noting that this
system dates back to the 8080 era. It is one of the things Microsoft
got right from the beginning, and I'm a little surprised that they
never got around to screwing it up.
QuickBasic / QBasic let you do it either way. If you need a static
10-character string, you can always DIM A AS STRING * 10.