Page 1 of 1

ProcedureReturn with pointers

Posted: Tue Jun 04, 2013 8:43 pm
by Kruno
In the manual it mentions that if you do not specify a return type that anything could return.
It also mentions you should not mix integers with pointers because of obvious issues with portability, i.e. win32 & win64 integer types and pointer sizes.

I like being able to return a pointer.
Can I really on this behaviour in PB if I am not concerned with portability?

Code: Select all

Structure AA
    I.i
EndStructure


Global NewList AAList.AA()

Procedure MakeAA(I.i)
    Global AAList
    AddElement(AAAList()): AAList()\I = I
    ProcedureReturn AAList()
EndProcedure


*Ass.Ass = MakeAA(12)
Debug *AA\I

Re: ProcedureReturn with pointers

Posted: Tue Jun 04, 2013 9:09 pm
by Demivec
Kruno wrote:I like being able to return a pointer.
Can I really on this behaviour in PB if I am not concerned with portability?
Yes. The default return type of a procedure is the integer type. This corresponds to the same type that a pointer would hold on either 32-bit or 64-bit, thus it is a portable type. Another similar type is the character (.c) type which changes depending on whether the compile mode is ASCII or Unicode.

I think your comment about the manual mentioning "that if you do not specify a return type that anything could return," refers to the situation where you do not supply a value after a 'ProcedureReturn' within the procedure. When you do not specify the return value after the ProcedureReturn it returns the value in either the CPU register eax or rax (depending on the 32/64-bit processor) and that means that "anything could return" if you did not purposely set these values to take advantage of this feature. The PureBasic commands use the CPU registers but don't leave them in a defined state so you get whatever was left over from the previous commands.

An example further demonstrating this:

Code: Select all

Procedure blah(x)
  If x
    ProcedureReturn 1
  EndIf
  
  ProcedureReturn ;no value specified, anything could be sent as the return value (but still an integer)
EndProcedure

Debug blah(1) ;defined value returned
Debug blah(0) ;undefined value returned

Re: ProcedureReturn with pointers

Posted: Tue Jun 04, 2013 9:20 pm
by Kruno
Thanks for clearing that up. :)