The stackalloc keyword is used in an unsafe code context to allocate a block of memory on the stack.
int* block = stackalloc int[100];
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];
Because pointer types are involved, stackalloc requires unsafe context. For more information, see Unsafe Code and Pointers (C# Programming Guide).
stackalloc is like _alloca in the C run-time library.
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.
class Test { static unsafe void Main() { const int arraySize = 20; int* fib = stackalloc int[arraySize]; int* p = fib; *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 - 1; ++i) { Console.WriteLine(fib[i]); } // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } /* Output 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 */
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.
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.
Concepts
Reference
Other Resources
Not to nitpick but this code example has a bug in it. On the first run, p[-2] points to the memory address space of the int before the initial pointer. The code assumes that the value in that address space is 0, but that's not always the case. Here's example output when running code in the debugger:
1
5875289
5875290
11750579
17625869
29376448
47002317
76378765
123381082
199759847
323140929
522900776
846041705
1368942481
-2079983110
-711040629
1503943557
792902928
-1998120811
Press any key to exit.
A quick way to fix this is by changing the code to look like this:
int* fib = stackalloc int[arraySize];
int* p = fib;
*p++ = 1;
*p++ = 1;
for (int i = 3; i < arraySize; ++i, ++p)
{
// Sum the previous two numbers.
*p = p[-1] + p[-2];
}
Edit by SJ at MSFT: See the current version of the topic (http://msdn.microsoft.com/en-us/library/cx9s2sy4.aspx ).