What functionality does the stackalloc keyword provide? When and Why would I want to use it?
-
"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
mallocornew. 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 theunsafesetting.stackallocallows 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.
TheTXI : StackOverflow is built so that people who DO search on the internet will find the answer here (it just has to be asked first).Adam Robinson : 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.Steve Haigh : Hmm. OK. Will edit then.Razzie : 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?'.Adam Robinson : You mean the exact phrasing of the title of this question...?TheTXI : @Razzie: Ouchies.PaulB : I'm curious now - what did Mr Haigh originally write :)TheTXI : @Paul: He basically said the author could have found this easily through an internet search.PaulB : If you don't like the phrasing please edit the questionRazzie : @Adam: hehe, didn't even read the title properly, it seems. That was silly of me :/Adam Robinson : @Razzie: No worries ;)Konrad Rudolph : “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.Steve Haigh : @Konrad - true. Will edit.Sung Meister : +1 for Edit2. Thanks -
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++)
-
stackallocdirects the .net runtime to allocate memory on the stack. -
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.
-
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.
Sung Meister : +1 for the stack and heap -
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 comments:
Post a Comment