Board index » delphi » Var.sized const arrays?

Var.sized const arrays?

Is there a way to create a constant array of strings that all have
variable lengths? At the moment I have:

const
  stuff1: array[1..10] of string[75] = (
    'fjfdklfjdkl',
    etc,
    'fdjfkdlfjfkljff');

The problem with this though is that it allocates 75 (the max allowance)
for EACH string, no matter if it's real length is less than 75 or not. My
code size would at least decrease by a few k if I could figure this out.

Any help would be appreciated! :)
                                          {*word*76} keeps drinking away
.oO(-- - --. .- -- -----)Oo.oO(-  - -)Oo. certain of its destination
l addr: te...@teleport.com l irc: [DiZ] l driving through New Orleans @ night
(Oo._ ___ _. ._ __ ______.oOo._ _  __.oO) gotta find a destination

 

Re:Var.sized const arrays?


Quote
Toni Poeltl (t...@sht.tuwien.ac.at) wrote:

: You could use C-like strings instead of pascal strings.

: const
:   stuff1: array[1..10] of PChar = (
:     'fjfdklfjdkl',
:     etc,
:     'fdjfkdlfjfkljff');

: This declaration causes borland pascal to embed the constants 'fklkdjf'
: etc into your .EXE file, reserving only the space they actually need, and
: to initialise the elements of the array stuff1 with the addresses of the
: strings.

I did figure that out, but I wasn't sure how to access them? As in how
would I move them to another destination? I have an array that I move to
the screen memory, and am at the moment moving strings to the array with an
asm method (so it converts the string to a [char][attr] format.)

thanks-

Re:Var.sized const arrays?


Quote
Toni Poeltl (t...@sht.tuwien.ac.at) wrote:

: Now you can access each character of the ith string via ds:si. Just
: increment si to point to the next character, but thats clear to you, I
: suppose. Don't forget that the first byte is not the string length and
: that the strings are null terminated, since we change from pascal strings
: to C-strings.

What if the pchar string contains a #0 in it?

                                         {*word*76} keeps drinking away
.oO(-- - --. .- -- ----)Oo.oO(-  - -)Oo. certain of its destination
l adr: te...@teleport.com l irc: [DiZ] l driving through New Orleans @ night
(Oo._ ___ _. ._ __ _____.oOo._ _  __.oO) gotta find a destination

Re:Var.sized const arrays?


Quote
>I did figure that out, but I wasn't sure how to access them? As in how
>would I move them to another destination? I have an array that I move to
>the screen memory, and am at the moment moving strings to the array with an
>asm method (so it converts the string to a [char][attr] format.)

Well, take a closer look at the array stuff1

stuff1: array[1..10] of PChar ...

This could be depicted as follows

stuff1:

  1   | address of 1. string | -----> 'fsakdfaljf'
  2   | address of 2. string | -----> 'lkdflkdadksjf'
  ...
  10  | address of 3. string | -----> 'dsklfjasl'

Suppose you want to access the ith (i being integer ) string, in particular you want to load the
address of the first char of that string into registers of the cpu, suppose ds:si, since you will
very likely use es:di for the destination.

I propose the following

  var
    i: integer

  {...}

  asm
    mov si,OFFSET stuff1   { load the base address of array stuff1}
    mov ax,i               { load ax with the index i}
    dec ax                 { the array starts with index 1}
    shl ax,1               { multiply by 4 ...
    shl ax,1               { which is the element size, SizeOf(PChar)=4 }
    add si,ax              { add it to si, now you can access the ith element in stuff1
                             via ds:si. I assumed that the strings are embedded in the code
                             segment, but they are in the data segment, which makes things easier }
    mov si,word ptr [si]   { load the offset of the first char of the ith string into ds:si }

  end;

Now you can access each character of the ith string via ds:si. Just increment si to point to the
next character, but thats clear to you, I suppose. Don't forget that the first byte is not the
string length and that the strings are null terminated, since we change from pascal strings to
C-strings.

Good luck

Other Threads