PaulB
PaulB

Reputation: 24382

When would I need to use the stackalloc keyword in C#?

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

Upvotes: 28

Views: 8990

Answers (7)

AnorZaken
AnorZaken

Reputation: 2124

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.

Upvotes: 2

Steve
Steve

Reputation: 8511

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.

Upvotes: 20

Aaron Schultz
Aaron Schultz

Reputation: 1224

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

Upvotes: 17

Deltax76
Deltax76

Reputation: 14263

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

Upvotes: 3

Adam Robinson
Adam Robinson

Reputation: 185643

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.

Upvotes: 9

Razzie
Razzie

Reputation: 31222

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.

Upvotes: 0

Mitch Wheat
Mitch Wheat

Reputation: 300579

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

Upvotes: 2

Related Questions