Skip to content

Latest commit

 

History

History
82 lines (50 loc) · 5.43 KB

File metadata and controls

82 lines (50 loc) · 5.43 KB
title stackalloc expression - Allocate variable storage on the stack instead of the heap
description The C# stackalloc expression allocates a block of memory on the stack. Stackalloc memory is automatically discarded when that method returns.
ms.date 06/16/2026
f1_keywords
stackalloc_CSharpKeyword
helpviewer_keywords
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 by stackalloc. A stack-allocated memory block isn't subject to garbage collection and doesn't need to be pinned by 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`1?displayProperty=nameWithType or xref:System.ReadOnlySpan`1?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`1 or xref:System.ReadOnlySpan`1 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`1 or xref:System.ReadOnlySpan`1 variable is allowed, as the following example shows:

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

    [!NOTE] Use xref:System.Span`1 or xref:System.ReadOnlySpan`1 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":::

    You must use an unsafe context when you work with pointer types.

    [!NOTE] The memory safety preview feature available in C# 15 lets you convert a stackalloc expression to a pointer outside an unsafe context. Operations that access the allocated memory through the pointer still require an unsafe context.

    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 by using stackalloc. For example, if the intended buffer size is below a certain limit, 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 before it's used, either with a stackalloc initializer or a method like xref:System.Span`1.Clear*?displayProperty=nameWithType.

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