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

后端 未结 7 962
清歌不尽
清歌不尽 2021-02-05 04:25

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

相关标签:
7条回答
  • 2021-02-05 04:36

    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 讨论(0)
  • 2021-02-05 04:38

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

    0 讨论(0)
  • 2021-02-05 04:42

    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.
    }
    
    0 讨论(0)
  • 2021-02-05 04:44

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

    0 讨论(0)
  • 2021-02-05 04:50

    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 讨论(0)
  • 2021-02-05 04:52

    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.

    0 讨论(0)
提交回复
热议问题