Dim for strings ? (qbasic)
Author Message
Dim for strings ? (qbasic)

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.

--
True knowledge is what you learn after you know everything.

Tue, 13 Apr 2004 02:52:35 GMT
Dim for strings ? (qbasic)

Hello Scott,

Quote:
> 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.

That is, if a pointer is Non-zero it points to a (variable-length) String,
otherwise there is no string at all (and a  non-existant string does not
need to be stored :-)

You can ofcourse declare an Array of fixed-length strings, forcing a number
of "empty" strings (Strings filled with Spaces) to be crated in
String-space, by using :

dim {string-name}({height},{width}) as string * {lenght}

For example :

dim a(3,4) as string * 5

will create 20 strings of 5 bytes each.

Regards,
Rudy Wieser

Tue, 13 Apr 2004 05:38:24 GMT
Dim for strings ? (qbasic)

Quote:
> 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.

For strings with _fixed_ length, use for example

DIM a(x%, y%) AS STRING * 256

Note: This allocates 256 bytes memory (beware the 640 KB conventional DOS
memory limit!) for every element. When you use

DIM a\$(x%, y%)

i.e. dynamic strings, only a field of pointers with lengths are allocated;
the actual string data are stored in the string heap memory. This memory
area must sometimes garbaged to free and compact all unused memory when
processing a lot of complex string operations.

Andreas

Tue, 13 Apr 2004 05:28:25 GMT
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.

Correct.  The array of pointers is stored in DGROUP space, and the
strings themselves are stored in string space.

<snip>
--
Arargh (at enteract dot com)    http://www.arargh.com

Tue, 13 Apr 2004 06:04:40 GMT
Dim for strings ? (qbasic)
Good overview, thanks.

Quote:

> > 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.

> For strings with _fixed_ length, use for example

> DIM a(x%, y%) AS STRING * 256

> Note: This allocates 256 bytes memory (beware the 640 KB conventional DOS
> memory limit!) for every element. When you use

> DIM a\$(x%, y%)

> i.e. dynamic strings, only a field of pointers with lengths are allocated;
> the actual string data are stored in the string heap memory. This memory
> area must sometimes garbaged to free and compact all unused memory when
> processing a lot of complex string operations.

>          Andreas

--
True knowledge is what you learn after you know everything.

Tue, 13 Apr 2004 08:03:09 GMT
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.

Tue, 13 Apr 2004 11:14:01 GMT
Dim for strings ? (qbasic)

Dim a(x,y) as string * length

Mike-

Quote:
>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.

-----------------------------------------------------------
Please note:  My public PGP key is available on my web site.
The web page is at http://www.catherders.com

Because network administration is like herding cats.
-----------------------------------------------------------
It is currently impossible to email me from any address in the
hotmail.com domain.  When they change their spam policies,
I'll change my trashcan filters.  Sorry for the inconvienence.
-----------------------------------------------------------

-----=  Posted via Newsfeeds.Com, Uncensored Usenet News  =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!