Page 2 of 2

Posted: Sat Aug 18, 2007 5:09 pm
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.

Posted: Sat Aug 18, 2007 5:24 pm
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)

Posted: Sat Aug 18, 2007 6:22 pm
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.