Page 1 of 1
Allocate Memory on Stack
Posted: Mon Oct 27, 2014 1:58 pm
by Shield
Hello Gents
Does anyone know a way of allocating memory on the stack
in the way the C-function 'alloca' does?
I figure that it's only possible with ASM (if it is even possible without compiler assistance), though I'd very much prefer
something standard like *foo = AllocateStackMemory(16).
Any thoughts are appreciated, thanks.

Re: Allocate Memory on Stack
Posted: Mon Oct 27, 2014 2:05 pm
by Fred
You should be able to do it with a structure of size 'X' (not tested, but you see the idea):
Code: Select all
Structure StackMemory16
a.b[16]
EndStructure
Procedure a()
*mem = @stackmem.StackMemory16
EndProcedure
Combine that with a macro to create your structure automatically depending of the size (if it's a fixed one), and it should be OK.
Re: Allocate Memory on Stack
Posted: Mon Oct 27, 2014 2:15 pm
by Shield
Oh wow, that's some smart trick.
Maybe with some macro magic I could be able to come up with something.
Thanks, Fred!
Re: Allocate Memory on Stack
Posted: Mon Oct 27, 2014 2:40 pm
by Thunder93
We often enough have to resort to tricks with PB.

Re: Allocate Memory on Stack
Posted: Mon Oct 27, 2014 10:20 pm
by idle
might need to resort to asm as it requires a constant or immediate value for the size
Code: Select all
Macro AllocateStackMemory(ptr,_size)
Structure stackmem_#_size
a.a[_size]
EndStructure
ptr = @stackmem.stackmem_#_size
EndMacro
Structure bArray
a.a[0]
EndStructure
Procedure foo()
Protected *foo.bArray
AllocateStackMemory(*foo,16)
For a = 0 To 15
*foo\a[a] = a
Next
For a = 0 To 15
Debug *foo\a[a]
Next
EndProcedure
foo()
foo()