Questions tagged [stackalloc]

C# operator that allocates a block of memory on the stack

34 questions
148
votes
5 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…
Groo
  • 45,930
  • 15
  • 109
  • 179
49
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
  • 927,783
  • 236
  • 2,422
  • 2,784
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
  • 13,565
  • 3
  • 27
  • 53
26
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
  • 20,984
  • 13
  • 54
  • 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
  • 8,813
  • 3
  • 30
  • 64
10
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,415
  • 2
  • 16
  • 33
9
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
  • 281
  • 1
  • 8
7
votes
0 answers

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 ?
7
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
  • 3,739
  • 1
  • 35
  • 58
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,510
  • 7
  • 16
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…
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
  • 11,352
  • 9
  • 65
  • 100
5
votes
3 answers

c++ allocation on the stack acting curiously

Curious things with g++ (maybe also with other compilers?): struct Object { Object() { std::cout << "hey "; } ~Object() { std::cout << "hoy!" << std::endl; } }; int main(int argc, char* argv[]) { { Object…
moala
  • 4,580
  • 6
  • 41
  • 64
4
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
  • 33,863
  • 5
  • 53
  • 91
4
votes
1 answer

Anonymous delegate not using new local for every iteration when data on local stackalloc

When using anonymous delegates in C# the CLR will generate a copy of the local (e.g. variables in the current scope) on the heap for used variables. Such a local will be put onto the heap for every declared variable of the current scope. You can see…
Matthias
  • 770
  • 1
  • 4
  • 20
1
2 3