Problem passing and copying fixed length WSTRING array

Windows specific questions.
Post Reply
PaulSquires
Posts: 1002
Joined: Jul 14, 2005 23:41

Problem passing and copying fixed length WSTRING array

Post by PaulSquires »

I thought that my logic was okay but I am getting gibberish printed after the copy. The code creates a fixed length wstring array in a sub, passes that array to another sub, and finally attempts to copy that array's data to a global/shared fixed length wstring array of the same size.

FB 1.05

I have tried to create code to demonstrate the problem. Here it is:

Code: Select all


Dim Shared m_rgNames(17) As WString * 20 => {"Auto", "Black", "Blue", "Green", "Cyan", "Red", _
   "Magenta", "Brown", "Light Gray", "Gray", "Light Blue", "Light Green", "Light Cyan", _
   "Light Red", "Light Magenta", "Yellow", "Bright White", "User selected..."}

Sub Foo2 ( prgNames() As WString * 20, ByVal numElements As Long)

   Dim i As Long
   
   For i = 0 To numElements - 1
      m_rgNames(i) = prgNames(i)
   Next

End Sub             

Sub Foo

   Dim rgNames(17) As WString * 20 => {"Auto", "Negro", "Azul", "Verde", "Cian", "Rojo", _
      "Magenta", "Marrón", "Gris claro", "Gris", "Azul claro", "Verde claro", "Cian claro", _
      "Rojo claro", "Magenta claro", "Amarillo", "Blanco", "Selección libre..."}

   Foo2( rgNames(), 18 )

End Sub           

' call Foo to initiate the copying of rgNames to global array via sub Foo2
Foo

Dim i As Long
For i = 0 To 17
   Print m_rgNames(i)
Next

Sleep
   
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Re: Problem passing and copying fixed length WSTRING array

Post by Zippy »

@Paul

Hmmm. You may be the first to try passing a Wstring array. Lucky you.

Abbreviated fail:

Code: Select all

Dim rgNames(17) As WString * 20 => {"Auto", "Negro", "Azul", "Verde", "Cian", "Rojo", _
   "Magenta", "Marrón", "Gris claro", "Gris", "Azul claro", "Verde claro", "Cian claro", _
   "Rojo claro", "Magenta claro", "Amarillo", "Blanco", "Selección libre..."}

'the assignment above is successful, I checked...

sub testit(prgNames() As WString * 20, ByVal numElements As Long)
   for i as long = 0 To numElements - 1
      print i, @prgNames(i), len(prgNames(i)),prgNames(i)
   next
end sub
'
testit(rgNames(),17)
'
sleep
May have to create array as a UDT and pass that?
.
PaulSquires
Posts: 1002
Joined: Jul 14, 2005 23:41

Re: Problem passing and copying fixed length WSTRING array

Post by PaulSquires »

Thanks Zippy - I am able to do the copy by simply using memcpy and copying (18 * 20 * 2) bytes from one array to the other. I was just surprised that I had trouble doing it using native FB statements.
fxm
Moderator
Posts: 12108
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Problem passing and copying fixed length WSTRING array

Post by fxm »

Known problem.
See at #650 BYREF/typedefs allow pointers to [Z/W]STRING * N

Workaround:

Code: Select all

Dim Shared m_rgNames(17) As WString * 20 => {"Auto", "Black", "Blue", "Green", "Cyan", "Red", _
   "Magenta", "Brown", "Light Gray", "Gray", "Light Blue", "Light Green", "Light Cyan", _
   "Light Red", "Light Magenta", "Yellow", "Bright White", "User selected..."}

Sub Foo2 ( prgNames() As WString * 20, ByVal numElements As Long)

   Dim i As Long
   
   For i = 0 To numElements - 1
      m_rgNames(i) = *(@prgNames(0) + i*20)  '' or m_rgNames(i) = (@prgNames(0))[i*20]
   Next

End Sub             

Sub Foo

   Dim rgNames(17) As WString * 20 => {"Auto", "Negro", "Azul", "Verde", "Cian", "Rojo", _
      "Magenta", "Marrón", "Gris claro", "Gris", "Azul claro", "Verde claro", "Cian claro", _
      "Rojo claro", "Magenta claro", "Amarillo", "Blanco", "Selección libre..."}

   Foo2( rgNames(), 18 )

End Sub           

' call Foo to initiate the copying of rgNames to global array via sub Foo2
Foo

Dim i As Long
For i = 0 To 17
   Print m_rgNames(i)
Next

Sleep
fxm
Moderator
Posts: 12108
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Problem passing and copying fixed length WSTRING array

Post by fxm »

Remark for dkl:
Pass by reference a (Z/W)string*N is well forbidden for a variable but not for an array?

Code: Select all

Declare Sub saz (arrayZ() As Zstring * 20)
Declare Sub saw (arrayW() As Wstring * 20)

Declare Sub sz (Z As Zstring * 20)
Declare Sub sw (W As Wstring * 20)
Compiler output:
.....\FBIDETEMP.bas(4) error 323: Fixed-length string combined with BYREF (not supported), before ')' in 'Declare Sub sz (Z As Zstring * 20)'
.....\FBIDETEMP.bas(5) error 323: Fixed-length string combined with BYREF (not supported), before ')' in 'Declare Sub sw (W As Wstring * 20)'
PaulSquires
Posts: 1002
Joined: Jul 14, 2005 23:41

Re: Problem passing and copying fixed length WSTRING array

Post by PaulSquires »

Thanks fxm, appreciate the information. I didn't realize it was already an identified issue. I will look through the bug tracker on sourceforge to familiarize myself with others.
fxm
Moderator
Posts: 12108
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Problem passing and copying fixed length WSTRING array

Post by fxm »

Large work: 150 bug reports still open!
Post Reply