C# operator that allocates a block of memory on the stack
Questions tagged [stackalloc]
41 questions
186
votes
6 answers
Practical use of `stackalloc` keyword
Has anyone ever actually used stackalloc while programming in C#? I am aware of what is does, but the only time it shows up in my code is by accident, because Intellisense suggests it when I start typing static, for example.
Although it is not…

vgru
- 49,838
- 16
- 120
- 201
53
votes
2 answers
Why does a zero-length stackalloc make the C# compiler happy to allow conditional stackallocs?
The following "fix" is very confusing to me; the scenario here is conditionally deciding whether to use the stack vs a leased buffer depending on the size - a pretty niche but sometimes-necessary optimization, however: with the "obvious"…

Marc Gravell
- 1,026,079
- 266
- 2,566
- 2,900
43
votes
1 answer
Should this unsafe code work also in .NET Core 3?
I'm refactoring my libraries to use Span for avoiding heap allocations if possible but as I target also older frameworks I'm implementing some general fallback solutions as well. But now I found a weird issue and I'm not quite sure whether I…

György Kőszeg
- 17,093
- 6
- 37
- 65
27
votes
7 answers
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?

PaulB
- 23,264
- 14
- 56
- 75
23
votes
2 answers
C# & .NET: stackalloc
I have a few questions about the functionality of the stackalloc operator.
How does it actually allocate? I thought it does something like:
void* stackalloc(int sizeInBytes)
{
void* p = StackPointer (esp);
StackPointer += sizeInBytes;
…

Jong
- 9,045
- 3
- 34
- 66
16
votes
2 answers
Initialization of memory allocated with stackalloc
If I'm allocating memory with stackalloc in C#, is that memory initialized (with 0)?
The documentation doesn't speak of that and only tells that the correct amount is reserved.
In my tests such memory defaulted to 0, but that doesn't mean it's…

frumbaela
- 391
- 1
- 8
13
votes
1 answer
Get pointer (IntPtr) from a Span staying in safe mode
I would like to use Span and stackalloc to allocate an array of struct and pass it to an interop call. Is it possible to retrieve a pointer (IntPtr) from the Span without being unsafe ?

Mitsuru Furuta
- 131
- 1
- 6
13
votes
4 answers
Why stackalloc cannot be used with reference types?
If stackalloc is used with reference types as below
var arr = stackalloc string[100];
there is an error
Cannot take the address of, get the size of, or declare a pointer to a
managed type ('string')
Why is so? Why CLR cannot declare pointer…

Konstantin Zadiran
- 1,485
- 2
- 22
- 35
8
votes
1 answer
Buffer overflow protection for stackalloc in .Net
From C# reference for stackalloc:
the use of stackalloc automatically enables buffer overrun detection features in the common language runtime (CLR). If a buffer overrun is detected, the process is terminated as quickly as possible to minimize the…

Roland Pihlakas
- 4,246
- 2
- 43
- 64
6
votes
1 answer
What is an efficient equivalent in C# for Span>, which does not exist?
I was porting some older high-speed C++ code to C#, and the existing code made use of a pointer-based double-indirection pattern like this (written here in a C# syntax), using the stack as efficient temporary storage:
public struct Source {
…

Sean Werkema
- 5,810
- 2
- 38
- 42
5
votes
1 answer
Why a `stackalloc` expression cannot be assigned to a `Span` parameter?
Consider the following methods (fiddle):
void test(Span param)
{
//Fail, the stackalloc'ed buffer could be exposed.
param = stackalloc int[10];
}
void test2(Span param)
{
//OK
Span local = stackalloc int[10];
}
I…

Margaret Bloom
- 41,768
- 5
- 78
- 124
5
votes
1 answer
How does stackalloc work?
Hello i am trying to figure out how does stackalloc work.So coming from C/C++ from my knowledge (limited) you can not allocate memory on the stack dynamically like in here:
C/C++ example:
void Allocate(int length){
int vector[length]; …

Bercovici Adrian
- 8,794
- 17
- 73
- 152
5
votes
1 answer
Why stackalloc accepts a variable length?
Any idea why the 'stackalloc' keyword accepts a variable length?
If this instruction returns a pointer to a buffer allocated in the stack's frame, how the compiler manage that? It recompiles the function at runtime every time it's called to organize…

andresantacruz
- 1,676
- 10
- 17
5
votes
1 answer
Pointer to struct containing System.Numerics.Vector in C#
I'm trying to make vector with 4 doubles with System.Numerics library because of SIMD. So I made this struct:
public struct Vector4D
{
System.Numerics.Vector vecXY, vecZW;
...
}
In this phase I code it for 128bit SIMD register.
It…

Sanquira Van Delbar
- 71
- 5
5
votes
2 answers
PIMPL and stack allocation
So I've been thinking about PIMPL and stack allocation. I've been writing a library and decided to use PIMPL to hide the private member of the class. That means I would have a class declared like this
class Foo {
private:
class Handle;
…

Anthony
- 12,177
- 9
- 69
- 105