[fpc-pascal] alloca

Hairy Pixels genericptr at gmail.com
Sat May 14 10:49:13 CEST 2022



> On May 14, 2022, at 1:03 PM, Michael Van Canneyt via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
> 
> As far as I can see, it can only be implemented in the compiler.
> Only the compiler knows how much stack it uses for a certain routine. the alloca needs to interact with this. FPC also has the nostackframe
> modifier, inline modifier: all things which in general interact with the
> stack.
> 
> The man page you refer to hints at this, it says the alloca function is
> compiler-dependent and machine-dependent. It names so many pitfalls that I
> would not recommend its use. If you think you need this, better declare an
> array of bytes of a sufficient size and use that.

Apparently what it does is advance the stack pointer x bytes and return a pointer to the new location so you could probably do that with some assembly. Of course this is dangerous because you can request more memory than exists in the current frame.

The reason I wanted to try this was because I saw some C++ example where the overrode the new operator and used alloca instead of malloc (like overriding NewInstance in FPC) and I thought this would be an interesting solution for some classes and just a fun experiment to play with.

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list