alloca Function (Builtin)

alloc.h, stdlib.h

void *alloca (unsigned long Size);

Allocates memory on the local storage space.

alloca is a function which is not included in the ANSI C standard, but it exists in many C dialects (including TIGCC). It allocates a block of Size bytes on the CPU stack (local storage space), in opposite to malloc which allocates memory on the memory heap. The space allocated with alloca exists until the containing function returns (i.e. it will be automatically deallocated at the end of the function). Be aware of the fact that the size of the hardware stack on TI calculators is limited to 16 kilobytes, so do not use alloca for allocating large blocks.

alloca is usually used for simulating automatic variable-sized one-dimensional arrays, which will be automatically deallocated when the function ends (like all automatic variables), so it is sometimes more preferable than malloc. For example, to simulate

int a[n];
where 'n' is not known in advance, you can use
int *a = alloca (n);
Note, however, that GNU C extensions allows variable-length arrays, which are more elegant than usage of alloca, and which may also be with more than one dimension (which is not possible with alloca).

Note: alloca is a built-in (open-coded) function in GNU C, which is translated to a single instruction which simply adjusts the stack. Some compiler command options (like '-ansi') prevent alloca from being an open-coded function. In this implementation of TIGCC, you can not use alloca with such options (however, it is very unlikely that you will ever have such problems).


Used by: SYMSTR