This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Astackalloc
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 withstackalloc
. A stack allocated memory block isn't subject togarbage collection and doesn't have to be pinned with afixed
statement.
You can assign the result of astackalloc
expression to a variable of one of the following types:
System.Span<T> orSystem.ReadOnlySpan<T>, as the following example shows:
int length = 3;Span<int> numbers = stackalloc int[length];for (var i = 0; i < length; i++){ numbers[i] = i;}
You don't have to use anunsafe context when you assign a stack allocated memory block to aSpan<T> orReadOnlySpan<T> variable.
When you work with those types, you can use astackalloc
expression inconditional or assignment expressions, as the following example shows:
int length = 1000;Span<byte> buffer = length <= 1024 ? stackalloc byte[length] : new byte[length];
You can use astackalloc
expression or a collection expression inside other expressions whenever aSpan<T> orReadOnlySpan<T> variable is allowed, as the following example shows:
Span<int> numbers = stackalloc[] { 1, 2, 3, 4, 5, 6 };var ind = numbers.IndexOfAny(stackalloc[] { 2, 4, 6, 8 });Console.WriteLine(ind); // output: 1Span<int> numbers2 = [1, 2, 3, 4, 5, 6];var ind2 = numbers2.IndexOfAny([2, 4, 6, 8]);Console.WriteLine(ind2); // output: 1
Note
We recommend usingSpan<T> orReadOnlySpan<T> types to work with stack allocated memory whenever possible.
Apointer type, as the following example shows:
unsafe{ int length = 3; int* numbers = stackalloc int[length]; for (var i = 0; i < length; i++) { numbers[i] = i; }}
As the preceding example shows, you must use anunsafe
context when you work with pointer types.
In the case of pointer types, you can use astackalloc
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, aStackOverflowException is thrown. To avoid that, follow the rules below:
Limit the amount of memory you allocate withstackalloc
. 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:
const int MaxStackLimit = 1024;Span<byte> buffer = inputLength <= MaxStackLimit ? stackalloc byte[MaxStackLimit] : new byte[inputLength];
Note
Because the amount of memory available on the stack depends on the environment in which the code is executed, be conservative when you define the actual limit value.
Avoid usingstackalloc
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 astackalloc
initializer, or a method likeSpan<T>.Clear before it's used.
Important
Not initializing memory allocated bystackalloc
is an important difference from thenew
operator. Memory allocated using thenew
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:
Span<int> first = stackalloc int[3] { 1, 2, 3 };Span<int> second = stackalloc int[] { 1, 2, 3 };ReadOnlySpan<int> third = stackalloc[] { 1, 2, 3 };// Using collection expressions:Span<int> fourth = [1, 2, 3];ReadOnlySpan<int> fifth = [1, 2, 3];
In expressionstackalloc T[E]
,T
must be anunmanaged type andE
must evaluate to a non-negativeint value. When you use thecollection expression syntax to initialize the span, the compiler may use stack allocated storage for a span if it won't violate ref safety.
The use ofstackalloc
automatically enables buffer overrun detection features in the common language runtime (CLR). If a buffer overrun is detected, the process is terminated as quickly as possible to minimize the chance that malicious code is executed.
For more information, see theStack allocation section of theC# language specification.
Was this page helpful?
Was this page helpful?