28

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

3
  • @Paul: I only revised the question to add a little bit more substance. There was nothing actually wrong with your initial phrasing.
    – TheTXI
    Commented Apr 6, 2009 at 13:04
  • Can someone illustrate when someone needs to use an unsafe operation in the answer as well?
    – dance2die
    Commented Apr 6, 2009 at 13:17
  • stackoverflow.com/questions/785226/… Commented Jan 8, 2013 at 17:18

7 Answers 7

20

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.

8
  • 2
    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
    Commented Apr 6, 2009 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. Commented Apr 6, 2009 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
    Commented Apr 6, 2009 at 12:57
  • 1
    You mean the exact phrasing of the title of this question...? Commented Apr 6, 2009 at 13:00
  • 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. Commented Apr 6, 2009 at 13:23
17

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
  • Note that due to escape analysis, managed may be allocated on the stack regardless.
    – user1804599
    Commented Jul 7, 2015 at 7:14
9

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.

0
3

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

2

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

2

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.