PureBasic Forum
http://forums.purebasic.com/english/

VarPtr for Purebasic
http://forums.purebasic.com/english/viewtopic.php?f=3&t=74024
Page 1 of 1

Author:  mk-soft [ Thu Nov 14, 2019 8:25 pm ]
Post subject:  VarPtr for Purebasic

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

Author:  Josh [ Thu Nov 14, 2019 9:24 pm ]
Post subject:  Re: VarPtr for Purebasic

A parameter transfer with ByRef would make more sense:

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

Author:  skywalk [ Thu Nov 14, 2019 9:54 pm ]
Post subject:  Re: VarPtr for Purebasic

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$)

Author:  NicTheQuick [ Fri Nov 15, 2019 10:58 am ]
Post subject:  Re: VarPtr for Purebasic

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)

Author:  Danilo [ Fri Nov 15, 2019 11:05 am ]
Post subject:  Re: VarPtr for Purebasic

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

Author:  skywalk [ Fri Nov 15, 2019 4:11 pm ]
Post subject:  Re: VarPtr for Purebasic

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?

Author:  mk-soft [ Fri Nov 15, 2019 8:37 pm ]
Post subject:  Re: VarPtr for Purebasic

@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

Author:  mk-soft [ Fri Nov 15, 2019 9:02 pm ]
Post subject:  Re: VarPtr for Purebasic

@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

Author:  skywalk [ Fri Nov 15, 2019 10:27 pm ]
Post subject:  Re: VarPtr for Purebasic

Excellent, this should be amplified in the manual. :!:

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/