Page 1 of 1

Best way to return pointer to allocated memory inside procedure?

Posted: Wed Nov 24, 2021 7:50 pm
by ricardo_sdl
Hi!
I need to allocate some memory inside a procedure and then return a pointer to it from the procedure, after some experimentation I arrived at these two methods:

Code: Select all

Procedure.i AllocateSomeMem()
  *Mem = AllocateMemory(1024)
  ProcedureReturn *Mem
EndProcedure

Procedure AllocateOtherMem(*Pointer)
  PokeI(*Pointer, AllocateMemory(1024))
EndProcedure

*MyPointer = #Null
AllocateOtherMem(@*MyPointer);pointer to pointer?
PokeS(*MyPointer, "other text here")
OtherText.s = PeekS(*MyPointer)
Debug OtherText
FreeMemory(*MyPointer)



*MyMem = AllocateSomeMem()
PokeS(*MyMem, "some text here")
Text.s = PeekS(*MyMem)
Debug Text
FreeMemory(*MyMem)

Is there any other way? Which one would be recommended? Is there a pointer to pointer in PureBasic, like in C for example?
Thanks!

Re: Best way to return pointer to allocated memory inside procedure?

Posted: Wed Nov 24, 2021 8:21 pm
by jacdelad
I would use the first one, though both are right. The variable *Something itself only stores the address, the memory is accessible from everywhere in your program. That's why you have to free it manually (or let it be done when the programs is exited).

Re: Best way to return pointer to allocated memory inside procedure?

Posted: Wed Nov 24, 2021 8:26 pm
by infratec
A pointer to a pointer like in C (**) is not directly possible.

Then you need something like

Code: Select all

*Pointer = PeekI(*PointerArray + SizeOf(Integer) * Index))

Re: Best way to return pointer to allocated memory inside procedure?

Posted: Wed Nov 24, 2021 8:33 pm
by Comfort
Try using an integer variable type:

Code: Select all

*MemPTR.integer 

To set or get:

Code: Select all

*MemPTR\i = AllocateMemory(1000)
Debug *MemPTR\i

Re: Best way to return pointer to allocated memory inside procedure?

Posted: Wed Nov 24, 2021 8:45 pm
by #NULL
@comfort, you would have to allocate *MemPTR first,, that's where we already are right now :)
Best way to return pointer to allocated memory inside procedure?
Without more information it would be

Code: Select all

Procedure p
  ProcedureReturn AllocateMemory(1)
EndProcedure

Re: Best way to return pointer to allocated memory inside procedure?

Posted: Wed Nov 24, 2021 8:53 pm
by Comfort
Pokes and Peeks drive me crazy, when there is a far elegant method available.

Re: Best way to return pointer to allocated memory inside procedure?

Posted: Wed Nov 24, 2021 11:54 pm
by mk-soft
Parameters ByRef

Code: Select all

;- Default vartypes

Procedure MyFunctionByRef(*Adress.integer, *dblVal.double)
  
  *Adress\i = AllocateMemory(1024)
  *dblVal\d = 99.1
  
EndProcedure

Define *mem, Value.d

MyFunctionByRef(@*mem, @Value)

Debug MemorySize(*mem)
Debug StrD(Value, 2)

;- Spezial for String. We need a Pointer where store the pointer to string
;  because the pointer to the string can changed

Procedure MyStringByRef(*strVal.string)
  
  *strVal\s = "*** " + *strVal\s + " ***"
  
EndProcedure

Define pText.string

pText\s = "Hello World"

Debug "Adress to string: " + @pText\s

MyStringByRef(@pText)

Debug pText\s
Debug "Adress to string: " + @pText\s

Re: Best way to return pointer to allocated memory inside procedure?

Posted: Thu Nov 25, 2021 12:45 pm
by ricardo_sdl
Thank you all for the answers! I think the one where the procedure returns the address is the easiest to read.