It is currently Sun Feb 16, 2020 8:21 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Passing string paramerter by reference to a procedure
PostPosted: Tue Nov 12, 2019 11:51 pm 
Offline
User
User

Joined: Thu May 02, 2019 3:57 am
Posts: 46
Location: USA
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:
Procedure.l GetErrorMessage(*AnErrorMessage.String)

  *AnErrorMessage\s = "This is the message"

EndProcedure


  S$ = " "
  *Pointer.String = @S$
  GetErrorMessage(*Pointer)
  debug *Pointer\s


Top
 Profile  
Reply with quote  
 Post subject: Re: Passing string paramerter by reference to a procedure
PostPosted: Wed Nov 13, 2019 12:00 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2220
Location: Germany
You need a Pointer to the Pointer of String

define Text$ is only Pointer to the String

Code:
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 / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Passing string paramerter by reference to a procedure
PostPosted: Wed Nov 13, 2019 12:10 am 
Offline
User
User

Joined: Thu May 02, 2019 3:57 am
Posts: 46
Location: USA
Thank you for your help. I got it to work. I was close... But you were closer.

Robert


Top
 Profile  
Reply with quote  
 Post subject: Re: Passing string paramerter by reference to a procedure
PostPosted: Wed Nov 13, 2019 12:29 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3464
Location: New Zealand
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:
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)


Top
 Profile  
Reply with quote  
 Post subject: Re: Passing string paramerter by reference to a procedure
PostPosted: Wed Nov 13, 2019 8:12 am 
Offline
Enthusiast
Enthusiast

Joined: Tue May 26, 2009 2:11 pm
Posts: 624
Why is Global for *StrE.String necessary as you provide the
adress by *StrE to the procedure?
It works without.

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Passing string paramerter by reference to a procedure
PostPosted: Wed Nov 13, 2019 7:38 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2220
Location: Germany
@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:
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 / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Passing string paramerter by reference to a procedure
PostPosted: Wed Nov 13, 2019 9:30 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2220
Location: Germany
Small Trick

Update
Code:

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 / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Passing string paramerter by reference to a procedure
PostPosted: Thu Nov 14, 2019 12:14 am 
Offline
Addict
Addict
User avatar

Joined: Mon Sep 15, 2003 5:55 am
Posts: 1341
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Passing string paramerter by reference to a procedure
PostPosted: Thu Nov 14, 2019 12:31 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2220
Location: Germany
@nco2k

that's wrong too.

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

Code:
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:
; 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 / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Passing string paramerter by reference to a procedure
PostPosted: Thu Nov 14, 2019 2:03 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3464
Location: New Zealand
thanks wasn't thinking


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye