What functionality does the stackalloc keyword provide? When and Why would I want to use it?
What functionality does the stackalloc keyword provide? When and Why would I want to
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From MSDN:
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.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.