Passing string parameters to subroutine

New to FreeBASIC? Post your questions here.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Passing string parameters to subroutine

Post by fzabkar »

I have a main program which reads a ROM image file of size dwROMsiz into a large string, sROMdata. I want to be able to parse this string with Instr in a subroutine.

I would prefer that the variables are not Shared, so that the subroutine would be portable. That is, I want to be able to insert the same subroutine, without modification, into other programs.

I want to avoid making a local copy of the string within the subroutine (the string could be 4MB in size), so I'm thinking that I could pass the size and string pointer as parameters rather than the string itself. However, how would I then use Instr to search for text data within the string?

I have a feeling that I'm misunderstanding some very fundamental concepts, but I can't put my finger on it.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Passing string parameters to subroutine

Post by grindstone »

You don't need any pointer, just pass the string ByRef:

Code: Select all

Sub parse(ByRef rom As String)
	Dim As Integer b, e
	Print Len(rom)
	Print
	b = 1
	Do
		e = InStr(b, rom, " ")
		Print Mid(rom, b, e - b)
		b = e + 1
	Loop While e
End Sub

Dim As String somedata = "The big brown fox jumped over the lazy dog"

parse(somedata)

Sleep
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Passing string parameters to subroutine

Post by fzabkar »

Thanks. I had wondered whether that was the solution, but it seems I never properly understood byref versus byval.
paul doe
Moderator
Posts: 1740
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Passing string parameters to subroutine

Post by paul doe »

fzabkar wrote: Jul 15, 2023 22:22 ...
I want to avoid making a local copy of the string within the subroutine (the string could be 4MB in size), so I'm thinking that I could pass the size and string pointer as parameters rather than the string itself. However, how would I then use Instr to search for text data within the string?

I have a feeling that I'm misunderstanding some very fundamental concepts, but I can't put my finger on it.
Indeed, that concept is called dereferencing:

Code: Select all

Sub parse(rom As String ptr)
	Dim As Integer b, e
	Print Len(*rom)
	Print
	b = 1
	Do
		e = InStr(b, *rom, " ")
		Print Mid(*rom, b, e - b)
		b = e + 1
	Loop While e
End Sub

Dim As String somedata = "The big brown fox jumped over the lazy dog"

parse(@somedata)

Sleep
The code is the same as above, but uses pointers. As you can see, 'dereferencing' simply means 'get the value that this pointer points to'.
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Passing string parameters to subroutine

Post by fzabkar »

Among other things, my conceptual problem was that I did not understand that a string pointer had a length property associated with it.

Thanks again.
fxm
Moderator
Posts: 12160
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Passing string parameters to subroutine

Post by fxm »

In FreeBASIC, a var-len string 's' has a descriptor under the hood ('@s' = address of its descriptor) that contains 3 data:
- address of the string character data,
- number of used character,
- number of usable characters.

Demonstrative example:

Code: Select all

Type descriptor
    Dim As Zstring Ptr address
    Dim As Integer used
    Dim As Integer usable
End Type

Dim As String s = "FreeBASIC"
Dim As descriptor Ptr ps = Cptr(Any Ptr, @s)

Print ps->address
Print ps->used
Print ps->usable
Print
Print *ps->address

Sleep

Passing (to a procedure) a string by reference means passing its address by value, and therefore passing the value of the address of its descriptor.
Post Reply