Getting address of string constants with @ or ?

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Except that it doesn't
Well, it does. Of course the pseudotype is necessary (else I wouldn't have included it), but it does work. Just don't pass a combined string (a+b) to the procedure.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Trond wrote:
Except that it doesn't
Well, it does. Of course the pseudotype is necessary (else I wouldn't have included it), but it does work. Just don't pass a combined string (a+b) to the procedure.
You didn't use a pseudotype you used a normal string type (.s).

Using this code from your function:

Code: Select all

Procedure Callback(FileAddress.l)
  Debug PeekL(FileAddress) ; Guaranteed to be different for each file
  Debug PeekS(PeekL(@FileAddress))
EndProcedure

Prototype ProtoCallbackInterface(FileAddress.s)

Function.ProtoCallbackInterface = @Callback()

string.s = "foo"
Debug "@string = " +Str(@string)
Debug "@"+Chr(34)+"foo"+Chr(34)+" = "+Str(@"foo")
Debug "Result of Function(string)"
Function(string)
Debug "Result of Function(@"+Chr(34)+"foo"+Chr(34)+")"
Function("foo")
Gives the following output:

Code: Select all

@string = 9045608
@"foo" = 4280824
Result of Function(string)
7274598
foo
Result of Function(@"foo")
7274598
foo
Notice that the address reported from your function in both cases is different from the original strings. PureBasic is copying the string data to the local variable. The original question was how to get the string address of the constant string (without copying it to a local buffer).

Worse still, if you return this value and try to use it you get an invalid memory access (put this code at the end of the above and run it):

Code: Select all

*foo.c = Function("foo")
Debug "*foo = "+Str(*foo)
Debug PeekS(*foo)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Sorry, I forgot an @. And when I add the @ I can remove the Peekl(). So this is the correct code (with example):

Code: Select all

Procedure Callback(FileAddress.l)
  Debug FileAddress ; Guaranteed to be different for each file
  Debug PeekS(FileAddress)
EndProcedure

Prototype ProtoCallbackInterface(FileAddress.s) 

Function.ProtoCallbackInterface = @Callback() 

string.s = "Hello"
Debug @string

Function(string)
Debug "---"
Function(#PB_Compiler_File)
PureBasic is copying the string data to the local variable.
Take a look at the generated assembly code to see what PureBasic is doing.
Post Reply