Relative Content

Tag Archive for c#.netgarbage-collectionstackalloc

Reinterpreting a stack allocated buffer to store managed pointers – will they interfere with GC?

I’m working with C# in a game engine and frequently need to pass around collections of objects to different functions. I’m running into cases where I only need to pass a subset of entries from some heap allocated collection, usually in the single to low double-digits. I am explicitly attempting to avoid allocating a new heap array for each call I make. I hoped it would be as simple as using stackalloc to make a small buffer wrapped by a Span and copy the references in, then pass that wherever it’s needed. This of course doesn’t work, as the the docs state that stackalloc can only be used with unmanaged types.