March 28, 2024, 10:36:22 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Dynamic string arrays

Started by REDEBOLT, May 30, 2007, 11:17:31 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

REDEBOLT

I have been experimenting with arrays of strings.

Consider the following static string array code:

string mystring[10]
mystring[5] = "Bob"
print "[",mystring[5],"]"


The result prints [Bob] as expected.   :)

Now consider dynamic array code:

pointer parray
parray = new(string,10) ' Allocate array.
' ZeroMemory(array, elements * size)
ZeroMemory(#<string>parray, 10 * 255)

#<string>parray[5] = "Bob  "
print "[",#<string>parray[5],"]"
print len(#<string>parray[5])

The result prints "B" with a length of 1.  Why?   ??? 

Now consider:

#<string>parray[5,5] = "Bob  "
print "[",#<string>parray[5,5],"]"
print len(#<string>parray[5,5])
delete parray

The result prints [Bob  ] as desired.   8)

Is the second subscript the string length? 
I am puzzled by the two subscripts.   ???

Here is my complete program:


$Main
autodefine "off"
openconsole
DECLARE IMPORT,ZeroMemory ALIAS RtlZeroMemory( POINTER pvoid,INT length),INT
'$Include "waitkey$.inc"
' WaitKey.inc
Declare IMPORT, Sleep(dwmillisecs As Int)
Sub WaitKey$(Opt raw=0 As Int),String
   String myinKey
   Do:Sleep(100):
      myinKey = InKey$(raw)
   Until myinKey<>""
   Return myinKey
End Sub
'
Int starttime,endtime
starttime = Timer

string mystring[10]
mystring[5] = "Bob"
print "[",mystring[5],"]"

pointer parray
parray = new(string,10) ' Allocate array.
' ZeroMemory(array, elements * size)
ZeroMemory(#<string>parray, 10 * 255)

#<string>parray[5] = "Bob  "
print "[",#<string>parray[5],"]"
print len(#<string>parray[5])

#<string>parray[5,5] = "Bob  "
print "[",#<string>parray[5,5],"]"
print len(#<string>parray[5,5])
delete parray

label EndItHere
endtime = Timer
endtime -= starttime
If endtime < 0 Then
    endtime += 86400    ' Calculate time past midnight.
endif

Print
Print "Elapsed Time:"
Print endtime," secs."
Print
Print "Done..."
Print "Press any key to quit."
WaitKey$()
closeconsole
End

???
Regards,
Bob

Ionic Wind Support Team

May 31, 2007, 07:09:49 AM #1 Last Edit: May 31, 2007, 07:23:45 AM by Paul Turley
String arrays are actually two dimensional, you don't notice because a static array is handled automatically by the compiler, it multiplies indexes for you.

When you allocate memory the type is only used to calculate the size of the allocated block and as far as the compiler is concerned it is just a contiguous array of bytes.  A pointer is just the address of memory, it does not contain the dimensions of the array.  You have to do the math yourself.


#<string>parray[5]

Accesses the 5th byte of the array, not the 5th string because a pointer does not contain dimensions, just an address.  In fact indexes in brackets won't do you much good with a block of memory.  The correct way to do it is with the * pointer operator and some pointer math...


pointer p
p = new(string,3)
*<string>(p+0)="hello"
*<string>(p+1*255)="There"
*<string>(p+2*255)="Robert"

for x = 0 to 2
print *<string>(p+x*255)," "
next x

do:until inkey$ <> ""
delete p


The * dereference operator allows treating any value as a pointer, including the result of adding a value to a pointer.

Paul.

Ionic Wind Support Team

REDEBOLT

Thanks, Paul, for the valuable info.
Sorry about your job, but I am glad you are back.

;D
Regards,
Bob