Show/Hide Toolbars

XSharp

The STACKALLOC keyword allows you allocate a block of memory on the stack of the current function / method

The syntax to use STACKALLOC is

 

VAR x := StackAlloc <dword>{1,2,3,4,5,6,7,8,9,10}
VAR y := StackAlloc int[]{10}

or more generic

VAR x := StackAlloc <typed Literal array>
VAR y := StackAlloc <typed array>

 

The compiler will generate variables of  the type "Typed PTR". So in the first example x will be of type DWORD PTR and y will be of type INT PTR.

 

You can also declare the variables with a normal LOCAL keyword. In that case the type must be <Type> PTR

 

LOCAL x := StackAlloc <dword>{1,2,3,4,5,6,7,8,9,10} AS DWORD PTR
LOCAL y := StackAlloc INT[]{10} AS INT PTR

 

You can also use STACKALLOC for an expression that is not a variable declaration. In that case the compiler will resolve the STACKALLOC expression to an allocation of an object of type System.Span<T>. This type is not available in the .Net Framework, but only in .Net 5 and later.