Allocating an array and passing it outside a procedure

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Allocating an array and passing it outside a procedure

Post by Mistrel »

Here is a neat solution I found for creating arrays inside procedures and then passing them out.

Code: Select all

Procedure MakeList()
	Static Dim Array(25)
	For i=1 To 25
		Array(i)=Random(255)
	Next i
	ProcedureReturn @array()
EndProcedure

Dim NewArray(0): aptr=MakeList()
Swap NewArray(),aptr

For i=1 To 25
	Debug NewArray(i)
Next i
It doesn't work the other way around though.

Code: Select all

Procedure MakeList(*Array)
	Static Dim LocalArray(0)
	Swap LocalArray(),*Array
	Size=PeekL(*Array-8) ; size of array
	Debug Size
	For i=1 To Size
		LocalArray(i)=Random(255)
	Next i
	Swap LocalArray(),*Array
EndProcedure

Dim Array(25)
MakeList(@Array())

For i=1 To 25
	Debug Array(i)
Next i