Passing string paramerter by reference to a procedure

Just starting out? Need help? Post your questions and find answers here.
RobertRioja
User
User
Posts: 71
Joined: Thu May 02, 2019 3:57 am
Location: USA
Contact:

Passing string paramerter by reference to a procedure

Post by RobertRioja »

How do I pass a string by reference to a procedure. In other words, how do I pass a pointer to a string so that the procedure can change the contents of the string?

I tried this but it did not work:

Code: Select all

Procedure.l GetErrorMessage(*AnErrorMessage.String)

  *AnErrorMessage\s = "This is the message"

EndProcedure


  S$ = " "
  *Pointer.String = @S$
  GetErrorMessage(*Pointer)
  debug *Pointer\s
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Passing string paramerter by reference to a procedure

Post by mk-soft »

You need a Pointer to the Pointer of String

define Text$ is only Pointer to the String

Code: Select all

Procedure.l GetErrorMessage(*AnErrorMessage.String)

  *AnErrorMessage\s = "This is the message"

EndProcedure


Define StringByRef.String

StringByRef\s = ""
  
GetErrorMessage(StringByRef)
Debug StringByRef\s

Structure udtText
  s.s
EndStructure

Define MyErrorText.udtText
GetErrorMessage(MyErrorText)
Debug MyErrorText\s


My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
RobertRioja
User
User
Posts: 71
Joined: Thu May 02, 2019 3:57 am
Location: USA
Contact:

Re: Passing string paramerter by reference to a procedure

Post by RobertRioja »

Thank you for your help. I got it to work. I was close... But you were closer.

Robert
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Passing string paramerter by reference to a procedure

Post by idle »

pb strings are stored in the datasection of your program which makes them a little funky
so you just need to allocate a buffer which is on the heap and it'll work as expected

Code: Select all

Procedure.i GetErrorMessage(*AnErrorMessage.string)
  
  *AnErrorMessage\s = "This is the message"
      
EndProcedure


Global *StrE.String = AllocateMemory(255*SizeOf(Character))
GetErrorMessage(*StrE)
Debug *StrE\s
FreeMemory(*strE) 
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Lord
Addict
Addict
Posts: 847
Joined: Tue May 26, 2009 2:11 pm

Re: Passing string paramerter by reference to a procedure

Post by Lord »

Why is Global for *StrE.String necessary as you provide the
adress by *StrE to the procedure?
It works without.
Image
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Passing string paramerter by reference to a procedure

Post by mk-soft »

@idle,

Your example is completely wrong and lead to the memory leak.

A pointer variable from Type String is a pointer on a pointer to the string.
After assigning a string, this then picks up the pointer to the string.

Code: Select all

Procedure.i GetErrorMessage(*AnErrorMessage.string)
  
  *AnErrorMessage\s = "This is the message"
      
EndProcedure

Debug "Size Of Type String: " + SizeOf(String)

Debug "Allocate Structure Of Type String"
Global *StrE.String = AllocateStructure(string)
Debug "Act Value of Pointer to String: " + PeekI(*StrE)
Debug "Call Function..."
GetErrorMessage(*StrE)
Debug "Act Value of Pointer to String: " + PeekI(*StrE)
Debug "PeekS String over Pointer: " + PeekS(PeekI(*StrE))
Debug "Default Output: " + *StrE\s

Debug "Free Structure: Is Freed String and Memory"
FreeStructure(*strE) 
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Passing string paramerter by reference to a procedure

Post by mk-soft »

Small Trick

Update

Code: Select all


Macro GetVarPtr(_Var_, _Pointer_)
  EnableASM
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    lea rax, _Var_
    mov _Pointer_, rax
  CompilerElse
    lea eax, _Var_
    mov _Pointer_, eax
  CompilerEndIf  
  DisableASM
EndMacro

Procedure.i GetErrorMessage(*AnErrorMessage.string)
  
  *AnErrorMessage\s = "This is the message - " + *AnErrorMessage\s
      
EndProcedure

Define text.s, *pText

text = "Hello World"
GetVarPtr(text, *pText)
GetErrorMessage(*pText)
Debug text
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: Passing string paramerter by reference to a procedure

Post by nco2k »

Code: Select all

Procedure.l GetErrorMessage(*AnErrorMessage)
  Protected *String.String = @*AnErrorMessage
  *String\s = "This is the message"
EndProcedure

S$ = " "
GetErrorMessage(@S$)
Debug S$
dunno if its safe to use though. i have no idea how pb's string builder deals with this internally.

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Passing string paramerter by reference to a procedure

Post by mk-soft »

@nco2k

that's wrong too.

You change the String at the Memory, not the Pointer to the String... Memory leak too.

Code: Select all

Procedure.l GetErrorMessage(*AnErrorMessage)
  Protected *String.String = @*AnErrorMessage
  *String\s = "This is the message" + *String\s
  Debug *String\s ; Bad String -> Memory leak
EndProcedure

S$ = "Test "
GetErrorMessage(@S$)
Debug S$
Right Methode like API

Code: Select all

; API Methode

Procedure.i GetErrorMessage(*AnErrorMessage, MaxLen)
  
  PokeS(*AnErrorMessage, Left("This is the message", MaxLen))
      
EndProcedure

text.s = Space(255)

GetErrorMessage(@text, Len(text))

Debug text

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Passing string paramerter by reference to a procedure

Post by idle »

thanks wasn't thinking
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply