简体   繁体   English

我什么时候需要在C#中使用stackalloc关键字?

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

What functionality does the stackalloc keyword provide? stackalloc关键字提供了哪些功能? When and Why would I want to use it? 何时以及为什么要使用它?

From MSDN : 来自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 . C#的一个主要特性是您通常不需要直接访问内存,就像使用mallocnew在C / C ++中一样。 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. 但是,如果你真的想明确地分配一些内存,那么C#认为这是“不安全的”,所以你只能在使用unsafe设置进行编译时才这样做。 stackalloc allows you to allocate such memory. stackalloc允许您分配这样的内存。

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. 除非你有特定的问题,并且不安全的代码是唯一的解决方案,否则你可能永远不需要这个。

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. Stackalloc将在堆栈上分配数据,这可用于避免通过在方法中重复创建和销毁值类型数组而生成的垃圾。

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

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 . 如果您对这意味着什么感兴趣,请查看此文章

http://msdn.microsoft.com/en-us/library/cx9s2sy4.aspx 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++) 通过使用它,您可以使用指针(C / C ++中强大而痛苦的功能)

stackalloc指示.net运行时在堆栈上分配内存。

Most other answers are focused on the "what functionality" part of OP's question. 大多数其他答案都集中在OP问题的“功能性”部分。
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. 现在在一个普通的应用程序中你不需要这个,但对于实时敏感的场景,它提供了更确定的性能:不涉及GC,你几乎可以保证缓存命中。
(Because worst-case performance is more important than average performance.) (因为最坏情况的表现比平均表现更重要。)

Keep in mind that the default stack size in .net is small though! 请记住,.net中的默认堆栈大小虽然很小!
(I think it's 1MB for normal apps and 256kb for ASP.net?) (我认为普通应用程序为1MB,ASP.net为256kb?)

Practical use could for example include realtime sound processing. 实际使用可以例如包括实时声音处理。

It is like Steve pointed out, only used in unsafe code context (eg, when you want to use pointers). 就像史蒂夫指出的那样,只用在不安全的代码上下文中(例如,当你想使用指针时)。

If you don't use unsafe code in your C# application, then you will never need this. 如果您不在C#应用程序中使用不安全的代码,那么您将永远不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM