stackalloc (C# Reference)

The stackalloc keyword is used in an unsafe code context to allocate a block of memory on the stack.

int* block = stackalloc int[100];

Remarks

The keyword is valid only in local variable initializers. The following code causes compiler errors.

int* block;
// The following assignment statement causes compiler errors. You
// can use stackalloc only when declaring and initializing a local
// variable.
block = stackalloc int[100];

Beginning with C# 7.3, you can use array initializer syntax for stackalloc arrays. All the following declarations declare an array with three elements whose values are the integers 1, 2, and 3:

// Valid starting with C# 7.3
int* first = stackalloc int[3] { 1, 2, 3 };
int* second = stackalloc int[] { 1, 2, 3 };
int* third = stackalloc[] { 1, 2, 3 };

Because pointer types are involved, stackalloc requires an unsafe context. For more information, see Unsafe Code and Pointers.

stackalloc is like _alloca in the C run-time library.

Examples

The following example calculates and displays the first 20 numbers in the Fibonacci sequence. Each number is the sum of the previous two numbers. In the code, a block of memory of sufficient size to contain 20 elements of type int is allocated on the stack, not the heap. The address of the block is stored in the pointer fib. This memory is not subject to garbage collection and therefore does not have to be pinned (by using fixed). The lifetime of the memory block is limited to the lifetime of the method that defines it. You cannot free the memory before the method returns.

const int arraySize = 20;
int* fib = stackalloc int[arraySize];
int* p = fib;
// The sequence begins with 1, 1.
*p++ = *p++ = 1;
for (int i = 2; i < arraySize; ++i, ++p)
{
    // Sum the previous two numbers.
    *p = p[-1] + p[-2];
}
for (int i = 0; i < arraySize; ++i)
{
    Console.WriteLine(fib[i]);
}
/* Output:
   1
   1
   2
   3
   5
   8
   13
   21
   34
   55
   89
   144
   233
   377
   610
   987
   1597
   2584
   4181
   6765
 */

The following example initializes a stackalloc array of integers to a bit mask with one bit set in each element. This demonstrates the new initializer syntax available starting in C# 7.3:

int* mask = stackalloc[] {
    0b_0000_0000_0000_0001,
    0b_0000_0000_0000_0010,
    0b_0000_0000_0000_0100,
    0b_0000_0000_0000_1000,
    0b_0000_0000_0001_0000,
    0b_0000_0000_0010_0000,
    0b_0000_0000_0100_0000,
    0b_0000_0000_1000_0000,
    0b_0000_0001_0000_0000,
    0b_0000_0010_0000_0000,
    0b_0000_0100_0000_0000,
    0b_0000_1000_0000_0000,
    0b_0001_0000_0000_0000,
    0b_0010_0000_0000_0000,
    0b_0100_0000_0000_0000,
    0b_1000_0000_0000_0000
};

for (int i = 0; i < 16; i++)
    Console.WriteLine(mask[i]);
/* Output:
   1
   2
   4
   8
   16
   32
   64
   128
   256
   512
   1024
   2048
   4096
   8192
   16384
   32768
 */

Security

Unsafe code is less secure than safe alternatives. However, the use of stackalloc 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.

C# language specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See also