It is currently Thu Dec 12, 2019 8:02 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: VarPtr for Purebasic
PostPosted: Thu Nov 14, 2019 8:25 pm 
Online
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2060
Location: Germany
The VarPtr function returns the start address of the memory area in which a variable is stored. Only for Profis.
Code:

Macro VarPtr(_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 MyFunction(*StringByRef.string)
 
  *StringByRef\s + " I Like PureBasic ;)"
     
EndProcedure

Define text.s, *pText

text = "Hello World!"
VarPtr(text, *pText)
MyFunction(*pText)
; MyFunction(VarPtr(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: VarPtr for Purebasic
PostPosted: Thu Nov 14, 2019 9:24 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 941
A parameter transfer with ByRef would make more sense:

Code:
Procedure MyFunction(ByRef MyText$)
   MyText$ + " I Like PureBasic ;)"
EndProcedure

_________________
sorry for my bad english


Top
 Profile  
Reply with quote  
 Post subject: Re: VarPtr for Purebasic
PostPosted: Thu Nov 14, 2019 9:54 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3153
Location: Boston, MA
There is an argument to be made a ByRef keyword is similar syntax when PB passes Arrays, Maps, Lists.
Procedure MyFunction(Array myArr(1))
Why not ?
Procedure MyFunction(ByRef myStr$)

_________________
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum


Top
 Profile  
Reply with quote  
 Post subject: Re: VarPtr for Purebasic
PostPosted: Fri Nov 15, 2019 10:58 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 22, 2003 7:43 pm
Posts: 461
Location: Germany, Saarbrücken
Interesting. So you can retrieve the Pointer of the variable itself instead of the string in it. That's nice.
Code:
Macro VarPtr(_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

Define text.s, *pText

text = "Hello World!"
VarPtr(text, *pText)

Debug "@text         = " + StrU(@text, #PB_Quad)
Debug "*pText        = " + StrU(*pText, #PB_Quad)
Debug "PeekI(*pText) = " + StrU(PeekI(*pText), #PB_Quad)

In the past I always created a structured string variable to make that happen, but of course that's ugly.
Code:
Procedure.i MyFunction(*StringByRef.string)
   
   *StringByRef\s + " I Like PureBasic ;)"
   
EndProcedure

Define text.String

text\s = "Hello World"

MyFunction(text)

Debug text\s

Works also with arrays, but is even uglier. :-D
Code:
Procedure.i MyFunction(*StringByRef.string)
   *StringByRef\s + " I Like PureBasic ;)"
EndProcedure

Dim text.s(9)

text(5) = "Hello World"

MyFunction(@text() + SizeOf(String) * 5)

Debug text(5)

_________________
Electronics, Crazy & Interesting Stuff, all that with text, image and sound? Click here!

The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.


Top
 Profile  
Reply with quote  
 Post subject: Re: VarPtr for Purebasic
PostPosted: Fri Nov 15, 2019 11:05 am 
Offline
Addict
Addict
User avatar

Joined: Sat Apr 26, 2003 8:26 am
Posts: 2942
Location: Planet Earth
Maybe you could just use the structure 'String' until it is implemented?
Code:
Macro VarPtr(_Var_)
    @_Var_
EndMacro

Procedure MyStringFunction(*StringByRef.String)
  *StringByRef\s + " I Like PureBasic ;)"
EndProcedure

Procedure MyIntegerFunction(*IntegerByRef.Integer)
  *IntegerByRef\i + 12
EndProcedure

Define text.String, x.i

text\s = "Hello World!"
x      = 10

MyStringFunction(  VarPtr(text) )
MyIntegerFunction( VarPtr(x)    )

Debug text\s
Debug x


Top
 Profile  
Reply with quote  
 Post subject: Re: VarPtr for Purebasic
PostPosted: Fri Nov 15, 2019 4:11 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3153
Location: Boston, MA
I thought the point of VarPtr() was to handle ByRef native strings without memory leak?
A structured variable works directly using @.
MyStringFunction(@text)
MyIntegerFunction(@x)
But not a native string?

_________________
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum


Top
 Profile  
Reply with quote  
 Post subject: Re: VarPtr for Purebasic
PostPosted: Fri Nov 15, 2019 8:37 pm 
Online
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2060
Location: Germany
@Danilo

Your interpretation of VarPtr is wrong.

VarPtr must return the address to the variable.
Yours is the address back to the value or string.
That's not how it works.

If you change the string, the pointer on the string can change. Therefore you need the address on the variable where the pointer is stored on the string to be able to pass it as ByRef to a procedure.

Edit:
This is actually only required for strings that are created as standard variables (global, define, etc).
For structures with strings and arrays as strings, you already have the variable where the pointer for the string is stored.

See Coding Question : viewtopic.php?f=13&t=74008

_________________
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: VarPtr for Purebasic
PostPosted: Fri Nov 15, 2019 9:02 pm 
Online
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2060
Location: Germany
@skywalk

I thought the point of VarPtr() was to handle ByRef native strings without memory leak? Yes

A structured variable works directly using @. Yes. We have a variable inside the structure with the pointer to string

MyStringFunction(@text) Direct access to String. We can't change strings. Only read

MyIntegerFunction(@x) Direct access to value. No problem. We don't need pointer, because that no changed

_________________
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: VarPtr for Purebasic
PostPosted: Fri Nov 15, 2019 10:27 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3153
Location: Boston, MA
Excellent, this should be amplified in the manual. :!:

_________________
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum


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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 5 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