Skip to content

Latest commit

 

History

History
79 lines (48 loc) · 5.18 KB

File metadata and controls

79 lines (48 loc) · 5.18 KB
title description ms.date f1_keywords helpviewer_keywords
stackalloc expression - Allocate variable storage on the stack instead of the heap
The C# stackalloc expression allocates a block of memory on the stack. Stackalloc memory is automatically discarded when that method returns.
01/20/2026
stackalloc_CSharpKeyword
stackalloc expression [C#]

stackalloc expression (C# reference)

A stackalloc expression allocates a block of memory on the stack. A stack-allocated memory block created during the method execution is automatically discarded when that method returns. You can't explicitly free the memory allocated with stackalloc. A stack allocated memory block isn't subject to garbage collection and doesn't have to be pinned with a fixed statement.

[!INCLUDEcsharp-version-note]

You can assign the result of a stackalloc expression to a variable of one of the following types:

  • xref:System.Span%601?displayProperty=nameWithType or xref:System.ReadOnlySpan%601?displayProperty=nameWithType, as the following example shows:

    :::code language="csharp" source="snippets/shared/StackallocOperator.cs" id="AssignToSpan":::

    You don't need to use an unsafe context when you assign a stack allocated memory block to a xref:System.Span%601 or xref:System.ReadOnlySpan%601 variable.

    When you work with those types, you can use a stackalloc expression in conditional or assignment expressions, as the following example shows:

    :::code language="csharp" source="snippets/shared/StackallocOperator.cs" id="AsExpression":::

    You can use a stackalloc expression or a collection expression inside other expressions whenever a xref:System.Span%601 or xref:System.ReadOnlySpan%601 variable is allowed, as the following example shows:

    :::code language="csharp" source="snippets/shared/StackallocOperator.cs" id="Nested":::

    [!NOTE] Use xref:System.Span%601 or xref:System.ReadOnlySpan%601 types to work with stack allocated memory whenever possible.

  • A pointer type, as the following example shows:

    :::code language="csharp" source="snippets/shared/StackallocOperator.cs" id="AssignToPointer":::

    As the preceding example shows, you must use an unsafe context when you work with pointer types.

    For pointer types, you can use a stackalloc expression only in a local variable declaration to initialize the variable.

The amount of memory available on the stack is limited. If you allocate too much memory on the stack, a xref:System.StackOverflowException is thrown. To avoid that exception, follow these rules:

  • Limit the amount of memory you allocate with stackalloc. For example, if the intended buffer size is below a certain limit, you allocate the memory on the stack; otherwise, use an array of the required length, as the following code shows:

    :::code language="csharp" source="snippets/shared/StackallocOperator.cs" id="LimitStackalloc":::

    [!NOTE] Because the amount of memory available on the stack depends on the environment in which the code runs, be conservative when you define the actual limit value.

  • Avoid using stackalloc inside loops. Allocate the memory block outside a loop and reuse it inside the loop.

The content of the newly allocated memory is undefined. You should initialize it, either with a stackalloc initializer, or a method like xref:System.Span%601.Clear%2A?displayProperty=nameWithType before it's used.

Important

Not initializing memory allocated by stackalloc is an important difference from the new operator. Memory allocated by using the new operator is initialized to the 0 bit pattern.

You can use array initializer syntax to define the content of the newly allocated memory. The following example demonstrates various ways to do that:

:::code language="csharp" source="snippets/shared/StackallocOperator.cs" id="StackallocInit":::

In expression stackalloc T[E], T must be an unmanaged type and E must evaluate to a non-negative int value. When you use the collection expression syntax to initialize the span, the compiler can use stack allocated storage for a span if it doesn't violate ref safety.

Security

Using stackalloc automatically turns on buffer overrun detection features in the common language runtime (CLR). If the runtime detects a buffer overrun, it terminates the process as quickly as possible to reduce the chance that malicious code runs.

C# language specification

For more information, see the Stack allocation section of the C# language specification.

See also