Bug in passing zstring array parameter to subroutine

General FreeBASIC programming questions.
Post Reply
lassar
Posts: 306
Joined: Jan 17, 2006 1:35

Bug in passing zstring array parameter to subroutine

Post by lassar »

I was trying to pass a zstring array to a subroutine

As you can see it does not print out the Menu Array.

Am I missing something?

Code: Select all


TYPE MString AS ZSTRING * 60

DECLARE SUB PrintMenu(MenuArray() AS MString)

DIM Menu(5) AS MString

Menu(1) = " In Order"
Menu(2) = " Random"
Menu(3) = " Missed"
Menu(4) = " All Missed"
Menu(5) = " Test"

FOR I% = 1 TO 5
    PRINT "Menu(I%) = ";Menu(I%)
NEXT I%

PRINT
PRINT "Press Anykey to Use PrintMenu function"
SLEEP

PrintMenu Menu()
SLEEP
END

SUB PrintMenu(MenuArray() AS MString)
    FOR I% = 1 TO 5
        PRINT "MenuArray(I%) = ";MenuArray(I%)
    NEXT I%
END SUB

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

Re: Bug in passing zstring array parameter to subroutine

Post by fxm »

You cannot simply pass a zstring array to a procedure because the zstring length is not passed (is passed 'as zstring * 1' by default).

If you want absolutely pass a zstring array, you are obliged to re-compute the address of each element in the procedure body!

Code: Select all

#Lang "fblite"

TYPE MString AS ZSTRING * 60

DECLARE SUB PrintMenu(MenuArray() AS MString)

DIM Menu(5) AS MString

Menu(1) = " In Order"
Menu(2) = " Random"
Menu(3) = " Missed"
Menu(4) = " All Missed"
Menu(5) = " Test"

FOR I% = 1 TO 5
    PRINT "Menu(I%) = ";Menu(I%)
NEXT I%

PRINT
PRINT "Press Anykey to Use PrintMenu function"
SLEEP

PrintMenu(Menu())
SLEEP
END

SUB PrintMenu(MenuArray() AS MString)
    FOR I% = 1 TO 5
        PRINT "MenuArray(I%) = ";(@MenuArray(0))[Sizeof(MString)*I%]
        'or PRINT "MenuArray(I%) = ";*(@MenuArray(0) + Sizeof(MString)*I%)
    NEXT I%
END SUB
Other formula if you want to kept the check of out-bounds on the array:
PRINT "MenuArray(I%) = ";*(@MenuArray(I%) + (Sizeof(MString)-1)*I%)
or:
PRINT "MenuArray(I%) = ";(@MenuArray(I%))[(Sizeof(MString)-1)*I%]
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Bug in passing zstring array parameter to subroutine

Post by dkl »

This is definitely a bug, related to #650. I can't say yet whether fixed-length string arrays passed as parameters can be implemented, but even if that's not possible then the compiler should show an error. At the moment it just fails silently...
lassar
Posts: 306
Joined: Jan 17, 2006 1:35

Re: Bug in passing zstring array parameter to subroutine

Post by lassar »

ZString arrays should be easy to implement.

Just do them similar to the way you do dynamic string arrays.

Basically, a array of pointers to the ZStrings themselves.
dodicat
Posts: 8238
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Bug in passing zstring array parameter to subroutine

Post by dodicat »

Wouldn't it be just as easy to use the Zstring pointer to begin with and pass this around?
Or am I missing something!

Code: Select all

#Lang "fblite"

Dim MString As zstring Ptr =Allocate(60)

Declare Sub PrintMenu(MenuArray() As Typeof(Mstring))

Dim Menu(5) As Typeof(MString)

Menu(1) = @" In Order"
Menu(2) = @" Random"
Menu(3) = @" Missed"
Menu(4) = @" All Missed"
Menu(5) = @" Test"

For I% = 1 To Ubound(menu)
    Print "Menu(I%) = ";*Menu(I%)
Next I%

Print
Print "Press Anykey to Use PrintMenu function"
Sleep

PrintMenu(Menu())
Sleep
End

Sub PrintMenu(MenuArray() As Typeof(MString))
    For I% = 1 To Ubound(MenuArray)
        Print "MenuArray(I%) = ";*Menuarray(I%)
    Next I%
    End Sub 
fxm
Moderator
Posts: 12548
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bug in passing zstring array parameter to subroutine

Post by fxm »

dodicat, the memory allocation (60 bytes) with as pointer 'MString' is useless (if we absolutely want declare an alternative name for 'Zstring Ptr', just define it with for example 'Type MString As Zstring Ptr'):

Code: Select all

#Lang "fblite"

Declare Sub PrintMenu(MenuArray() As Zstring Ptr)

Dim Menu(5) As Zstring Ptr

Menu(1) = @" In Order"
Menu(2) = @" Random"
Menu(3) = @" Missed"
Menu(4) = @" All Missed"
Menu(5) = @" Test"

For I% = 1 To Ubound(menu)
    Print "Menu(I%) = ";*Menu(I%)
Next I%

Print
Print "Press Anykey to Use PrintMenu function"
Sleep

PrintMenu(Menu())
Sleep
End

Sub PrintMenu(MenuArray() As Zstring Ptr)
    For I% = 1 To Ubound(MenuArray)
        Print "MenuArray(I%) = ";*Menuarray(I%)
    Next I%
End Sub
dodicat
Posts: 8238
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Bug in passing zstring array parameter to subroutine

Post by dodicat »

Yea, OK fxm.
I was trying to simulate 60 characters, silly me.
fxm
Moderator
Posts: 12548
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bug in passing zstring array parameter to subroutine

Post by fxm »

Another version with dynamic allocation for the zstring character data:

Code: Select all

#Lang "fblite"

Dim MenuPtr(5) AS Zstring Ptr
For I% = 1 To 5
  MenuPtr(I%) = Callocate(60)
Next I%

DECLARE SUB PrintMenu(MenuArrayPtr() AS Zstring Ptr)

*MenuPtr(1) = " In Order"
*MenuPtr(2) = " Random"
*MenuPtr(3) = " Missed"
*MenuPtr(4) = " All Missed"
*MenuPtr(5) = " Test"

FOR I% = 1 TO 5
    PRINT "*MenuPtr(I%) = ";*MenuPtr(I%)
NEXT I%

PRINT
PRINT "Press Anykey to Use PrintMenu function"
SLEEP

PrintMenu(MenuPtr())
SLEEP
For I% = 1 To 5
  Deallocate(MenuPtr(I%))
Next I%
END

SUB PrintMenu(MenuArrayPtr() AS Zstring Ptr)
    FOR I% = 1 TO 5
        PRINT "*MenuArrayPtr(I%) = ";*MenuArrayPtr(I%)
    NEXT I%
END SUB
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Bug in passing zstring array parameter to subroutine

Post by TJF »

Why do you CALLOCATE / DEALLOCATE memory for static strings (menu entries doesn't change)?

Code: Select all

SUB PrintMenu(MenuArrayPtr() AS ZSTRING PTR)
  FOR i AS INTEGER = 0 TO UBOUND(MenuArrayPtr)
    PRINT "*MenuPtr(i) = "; *MenuArrayPtr(i)
  NEXT
END SUB

DIM AS ZSTRING PTR MenuPtr(...) = _
  { @" In Order" _
  , @" Random" _
  , @" Missed" _
  , @" All Missed" _
  , @" Test" _
  }


FOR i AS INTEGER = 0 TO UBOUND(MenuPtr)
  PRINT "*MenuPtr(i) = "; *MenuPtr(i)
NEXT

PRINT
PRINT "Press Anykey to Use PrintMenu function"
SLEEP

PrintMenu(MenuPtr())
SLEEP
fxm
Moderator
Posts: 12548
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bug in passing zstring array parameter to subroutine

Post by fxm »

Why only a static menu?
This can be a context-sensitive menu (eg like the right mouse button!).
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Bug in passing zstring array parameter to subroutine

Post by TJF »

fxm wrote:Why only a static menu?
Because it's best practise.
fxm wrote:This can be a context-sensitive menu (eg like the right mouse button!).
In that case I use multiple pointers for changeable entries. And for variable text I use placeholders in the static strings.
Post Reply