Best way to return pointer to allocated memory inside procedure?

Just starting out? Need help? Post your questions and find answers here.
ricardo_sdl
Enthusiast
Enthusiast
Posts: 109
Joined: Sat Sep 21, 2019 4:24 pm

Best way to return pointer to allocated memory inside procedure?

Post 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!
You can check my games at:
https://ricardo-sdl.itch.io/
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

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

Post 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).
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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))
Comfort
User
User
Posts: 22
Joined: Thu Jul 05, 2018 11:52 pm

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

Post 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
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

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

Post 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
Comfort
User
User
Posts: 22
Joined: Thu Jul 05, 2018 11:52 pm

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

Post by Comfort »

Pokes and Peeks drive me crazy, when there is a far elegant method available.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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
Last edited by mk-soft on Thu Nov 25, 2021 1:03 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
ricardo_sdl
Enthusiast
Enthusiast
Posts: 109
Joined: Sat Sep 21, 2019 4:24 pm

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

Post by ricardo_sdl »

Thank you all for the answers! I think the one where the procedure returns the address is the easiest to read.
You can check my games at:
https://ricardo-sdl.itch.io/
Post Reply