Allocate Memory on Stack

Everything else that doesn't fall into one of the other PB categories.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Allocate Memory on Stack

Post 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. :)
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Fred
Administrator
Administrator
Posts: 18547
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Allocate Memory on Stack

Post 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.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Allocate Memory on Stack

Post 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!
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
Thunder93
Addict
Addict
Posts: 1790
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Allocate Memory on Stack

Post by Thunder93 »

We often enough have to resort to tricks with PB. :lol:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
idle
Always Here
Always Here
Posts: 6238
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Allocate Memory on Stack

Post 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()
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply