What functionality does the stackalloc
keyword provide? When and Why would I want to use it?
-
@Paul: I only revised the question to add a little bit more substance. There was nothing actually wrong with your initial phrasing. – TheTXI Apr 06 '09 at 13:04
-
Can someone illustrate when someone needs to use an unsafe operation in the answer as well? – dance2die Apr 06 '09 at 13:17
-
http://stackoverflow.com/questions/785226/practical-use-of-stackalloc-keyword – Mauricio Scheffer Jan 08 '13 at 17:18
7 Answers
From MSDN:
Used in an unsafe code context to allocate a block of memory on the stack.
One of the main features of C# is that you do not normally need to access memory directly, as you would do in C/C++ using malloc
or new
. However, if you really want to explicitly allocate some memory you can, but C# considers this "unsafe", so you can only do it if you compile with the unsafe
setting. stackalloc
allows you to allocate such memory.
You almost certainly don't need to use it for writing managed code. It is feasible that in some cases you could write faster code if you access memory directly - it basically allows you to use pointer manipulation which suits some problems. Unless you have a specific problem and unsafe code is the only solution then you will probably never need this.

- 16,495
- 3
- 60
- 86

- 8,469
- 1
- 26
- 37
-
2StackOverflow is built so that people who DO search on the internet will find the answer here (it just has to be asked first). – TheTXI Apr 06 '09 at 12:52
-
Unfortunately you did sound rude :) Perhaps this person doesn't understand the difference between allocating memory on the stack vs. the heap. Your answer was obvious and uncalled for. – Adam Robinson Apr 06 '09 at 12:53
-
Adam, I don't necessarily agree. He *probably* would've phrased his question differently then, for example something like 'When would I need to use the stackalloc keyword?'. – Razzie Apr 06 '09 at 12:57
-
1
-
-
@Paul: He basically said the author could have found this easily through an internet search. – TheTXI Apr 06 '09 at 13:02
-
@Adam: hehe, didn't even read the title properly, it seems. That was silly of me :/ – Razzie Apr 06 '09 at 13:08
-
2“is not managed by the CLR” – this is correct but very misleading because since it's allocated on the stack it automatically gets removed at the end of the scope/method and there's no need for any further garbage collection. – Konrad Rudolph Apr 06 '09 at 13:23
Stackalloc will allocate data on the stack, which can be used to avoid the garbage that would be generated by repeatedly creating and destroying arrays of value types within a method.
public unsafe void DoSomeStuff()
{
byte* unmanaged = stackalloc byte[100];
byte[] managed = new byte[100];
//Do stuff with the arrays
//When this method exits, the unmanaged array gets immediately destroyed.
//The managed array no longer has any handles to it, so it will get
//cleaned up the next time the garbage collector runs.
//In the mean-time, it is still consuming memory and adding to the list of crap
//the garbage collector needs to keep track of. If you're doing XNA dev on the
//Xbox 360, this can be especially bad.
}

- 1,188
- 1
- 8
- 9
-
Note that due to escape analysis, `managed` may be allocated on the stack regardless. – Jul 07 '15 at 07:14
Paul,
As everyone here has said, that keyword directs the runtime to allocate on the stack rather than the heap. If you're interested in exactly what this means, check out this article.

- 182,639
- 35
- 285
- 343
http://msdn.microsoft.com/en-us/library/cx9s2sy4.aspx
this keyword is used to work with unsafe memory manipulation. By using it, you have ability to use pointer (a powerful and painful feature in C/C++)

- 13,931
- 30
- 95
- 128
Most other answers are focused on the "what functionality" part of OP's question.
I believe this will answers the when and why:
When do you need this?
For the best worst-case performance with cache locality of multiple small arrays.
Now in an average app you won't need this, but for realtime sensitive scenarios it gives more deterministic performance: No GC is involved and you are all but guaranteed a cache hit.
(Because worst-case performance is more important than average performance.)
Keep in mind that the default stack size in .net is small though!
(I think it's 1MB for normal apps and 256kb for ASP.net?)
Practical use could for example include realtime sound processing.

- 1,916
- 1
- 22
- 34
stackalloc
directs the .net runtime to allocate memory on the stack.

- 295,962
- 43
- 465
- 541
It is like Steve pointed out, only used in unsafe code context (e.g, when you want to use pointers).
If you don't use unsafe code in your C# application, then you will never need this.

- 30,834
- 11
- 63
- 78