C# Reference ยท Operators and Expressions
The stackalloc operator
Reference for C# stackalloc: allocate a short-lived array on the stack as a Span, size limits, initialization, safety rules and when to avoid it.
stackalloc allocates a block of memory on the call stack instead of the heap. The memory is freed automatically when the method returns, and the garbage collector never sees it. That makes it very cheap for small, short-lived buffers in hot code.
In modern C# you assign the result to a Span<T> or ReadOnlySpan<T>, which is safe and needs no unsafe keyword. The element type must be an unmanaged type (numbers, char, bool, and structs made only of such fields).
Span<int> buffer = stackalloc int[16]; // 16 zeroed ints
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
Span<T> s = stackalloc T[length]; Span<T> s = stackalloc T[] { a, b, c }; Span<T> s = stackalloc[] { 1, 2, 3 }; // T inferred T* p = stackalloc T[length]; // pointer form, needs unsafe
| Name | Description |
|---|---|
T |
An unmanaged element type. |
length |
Number of elements. Any non-negative integer expression. |
Returns: a Span<T> (or pointer) over the new memory, zero-initialized unless an initializer is used.
Errors: a negative length throws OverflowException. Too much stack space terminates the process with a stack overflow, which cannot be caught.
Usage notes
- The default stack is about 1 MB on most .NET platforms (8 MB on Linux main threads), shared by every method on the call chain. Keep
stackallocbuffers small, well under a few kilobytes, and fall back to the heap orArrayPool<T>for larger sizes. - Never
stackallocinside a loop with a growing size: the memory is released only when the method returns, not at the end of each iteration. - The span cannot escape the method. You cannot return it, store it in a field of a class, or capture it in a lambda or async method. This is enforced because
Span<T>is aref struct. - Do not use it for variable, user-controlled sizes without a limit; that is a denial-of-service risk.
- The pattern in the second example (
stackallocfor small, array for large) is the standard way to combine speed and safety. - Since C# 7.2 you may use
stackallocin a conditional expression as shown. Before that it was only allowed as an initializer of a local variable. [SkipLocalsInit]on a method removes the zeroing for extra speed, but then you must write every element before reading it.- Strings and arrays are reference types and cannot be allocated this way. Use
InlineArray(C# 12) for fixed-size buffers inside structs.