tags:

views:

961

answers:

6

What functionality does the stackalloc keyword provide? When and Why would I want to use it?

+10  A: 

"Used in an unsafe code context to allocate a block of memory on the stack". From MSDN.

Edit A bit more explanation. 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.

Edit 2 When and why would you want to use it? My advice, 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.

Steve Haigh
StackOverflow is built so that people who DO search on the internet will find the answer here (it just has to be asked first).
TheTXI
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
Hmm. OK. Will edit then.
Steve Haigh
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
You mean the exact phrasing of the title of this question...?
Adam Robinson
@Razzie: Ouchies.
TheTXI
I'm curious now - what did Mr Haigh originally write :)
PaulB
@Paul: He basically said the author could have found this easily through an internet search.
TheTXI
If you don't like the phrasing please edit the question
PaulB
@Adam: hehe, didn't even read the title properly, it seems. That was silly of me :/
Razzie
@Razzie: No worries ;)
Adam Robinson
“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
@Konrad - true. Will edit.
Steve Haigh
+1 for Edit2. Thanks
Sung Meister
+2  A: 

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++)

Vimvq1987
+1  A: 

stackalloc directs the .net runtime to allocate memory on the stack.

Mitch Wheat
A: 

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.

Razzie
+5  A: 

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.

Adam Robinson
+1 for the stack and heap
Sung Meister
+4  A: 

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.
}
Aaron Schultz