Question about Span, stackalloc, and structs.

Hey, not sure if this is the right place to ask, but here is the question:

I am stoked about the new System.Memory project with ref returns and safe stackalloc. The lack of these features made writing latency critical code in C# nearly impossible. It also caused the garbage collector to have to work much harder than was really necessary.

My question is, how does the new design handle fixed arrays within a struct? Lets say I need an array of 6 doubles in a struct to implement a vector that I want to stackalloc. Right now I could use the “fixed” keyword, but that would make the struct unsafe. If I just use a double[] in the struct, that would end up in the garbage collected heap. Does C# 7.2 make the fixed keyword safe as well, or is there another way to implement this?

edit: I don’t need a workaround or another option for allocating native or pool memory. This is for specialized applications where stack memory needs to be used directly.

submitted by /u/robotdude12
[link] [comments]

Leave a Reply