Search code examples
c#unsafe

Get bytes from float array without allocations (i.e. via cast)


I have a quite large float array (usually >40 million entries) which takes up between 150MB und 250MB megabytes in memory and I need to pass it to two different APIs. Both are thrid party tools which I can't change. One of which only accepts byte[] and the other one only ref float[].

It is not a problem for me to convert it to a byte[] using Buffer.BlockCopy or similar approaches. But due to memory fragmentation allocating an array of this size fails regularly, and I would like to avoid copying it if possible.

Is there any way, to accomplish this? Maybe using unsafe code or C++ cli or C++ or a combination of all of them? Or maybe using MemoryMappedFile? I have already tried all of these but so far without any success.


Solution

  • This question was previously tagged C++/CLI. In that context, byte[] is somewhat ambiguous, you could be referring to a unmanaged C-style array (which uses the [] syntax in C++/CLI), or you could be referring to a managed array (which uses the [] syntax in C#, but not in C++/CLI.)

    Since the question is now tagged only C#, I'm assuming that when you say [], you're referring to managed arrays.

    There is no way to make two managed arrays share the same memory.

    If the two APIs you need to use are byte[] and float[] in C#, there's no way to make them share the memory. If one of the APIs took something lower-level, such as a byte*, there would probably be a way, but an array in C# is a full-fledged object, and the only thing it wants to do is allocate its own memory.