Skip to main content

Questions tagged [stackalloc]

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

stackalloc
5 votes
1 answer
198 views

What is the most efficient way to create a temporary collection? Differences between stackalloc, and collection expressions?

In C# there are now multiple ways to create a temporary collection. You can just new an array and hope the garbage collector doesn't mind. // Just create a new array Span<int> array = new int[]{ ...
Roy T.'s user avatar
  • 9,558
1 vote
0 answers
104 views

C#: of two methods which calculate Levenshtein distance, why does the heap-allocated "cost" array outperform the stack-allocated "cost" array?

Consider the following C# implementation (not my own) for Levenshtein distance: "Fastenshtein/StaticLevenshtein.cs". I removed the original comments and eliminated some whitespace for ...
chase.stone's user avatar
2 votes
0 answers
146 views

unknown function in decompiled dll

I'm trying to decompile a dll which is in .net 4.6. After using de4dot to decompile and deobfuscate the dll there are bunch of classes in it. I came across two functions(?) named __fieldref and ...
Fardin's user avatar
  • 33
7 votes
1 answer
390 views

What is an efficient equivalent in C# for Span<Span<T>>, 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 ...
Sean Werkema's user avatar
  • 6,006
0 votes
1 answer
78 views

Rapid stack allocations vs accessing a single heap allocation

I'm having a situation where I have an array T[] which must be copied in an instant and sent over to a function accepting a ReadOnlySpan<T>. I found two solutions on this problem. However I'm ...
Damian's user avatar
  • 137
3 votes
0 answers
398 views

Can I suppress .local for stackalloc in C# without using unsafe?

To allocate a double array initialized to zero, you can choose var x = new double[N]; to allocate on the heap and Span<double> x = stackalloc double[N]; to allocate on the stack. To suppress ...
kolbe's user avatar
  • 101
0 votes
0 answers
55 views

Is there a way, to conditionally allocate some fixed-size-stack buffer?

pretty much tittle! imagine this here: public unsafe ConditionalAllocOnStack { private fixed byte STACK[CONDITION_TRUE ? 100 : 0]; public ConditionalAllocOnStack() { //.... } ...
Shpendicus's user avatar
0 votes
0 answers
83 views

Garbage data after stackalloc field initializer

I am using .NET 6.0 on Windows 10 with Visual Studio 2022 last Version, last Build, and this code runs fine and even SEEMINLGY does what I want: have a look: But: keep a closer look on the "Init(...
Shpendicus's user avatar
4 votes
1 answer
265 views

What is the return type of a stackalloc espression?

I'm playing with stackalloc and finding a lot of weirdness in its return type. Here are some examples using stackalloc<float>: 1. Implicit typing returns float*: var a = stackalloc float[1];//a ...
Nigel's user avatar
  • 3,161
1 vote
1 answer
138 views

What happens when I assign a `ref` value to an `out` argument in C#?

I am writing an class that retrieves binary data and supports generically converting it into primitive types. It's supposed to be as efficient as possible. Here is what it looks like right now: public ...
TheLeftExit's user avatar
5 votes
1 answer
750 views

Why a `stackalloc` expression cannot be assigned to a `Span<T>` parameter?

Consider the following methods (fiddle): void test(Span<int> param) { //Fail, the stackalloc'ed buffer could be exposed. param = stackalloc int[10]; } void test2(Span<int> param) {...
Margaret Bloom's user avatar
53 votes
2 answers
4k views

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-...
Marc Gravell's user avatar
0 votes
0 answers
268 views

Why do I have padding in the stack?

I was trying to understand how the stack (segment) works and thought that it will simply allocate each element (variable, byte, whatever I want to allocate) one after another. BUT after writing ...
user avatar
0 votes
1 answer
972 views

How to use C# stackalloc and keep the same code structure?

I have a method MemRead that reads memory and returns a byte array [DllImport("kernel32.dll", SetLastError = true)] internal static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,...
KVM's user avatar
  • 910
0 votes
1 answer
360 views

Allocation-free enumeration and processing

I would like to solve the massive allocation costs of a c# application. The application itself can be represented by the TickUser class at the bottom, and I'd like to know how to implement the ...
bboyle1234's user avatar
  • 4,949

15 30 50 per page