Search code examples
c++serializationfloating-pointieee-754webassembly

Serializing floats/ doubles in C++, compiled to WebAssembly


I am trying to serialize IEEE 754 floats/ doubles to byte arrays in my C++ code that is then compiled to WebAssembly. (This code then runs as an eosio smart contract, but I believe that this shouldn't be relevant)

My problem is that every solution I find that works in C++ doesn't work anymore after compiling it to WebAssemby, giving me the error cause: access violation\n${callstack}

I have tried unions, reinterpret_cast and memcpy, all of which gave me the error seen above. I don't really understand how Webassembly works, but my educated guess would be that Webassembly doesn't allow direct memory access like C++ does, and therefore there is no proper way for the compiler to translate the "standard" C++ solutions.

Do you know of any way to solve this problem. Are there perhaps C++ libraries that serialize arrays without using any solutions that directly access the memory? And if there aren't, do you perhaps have any ideas as to how you would solve this problem?


Solution

  • The errors actually had nothing to do with the float/ double serialization but rather with the string to float/ double functions, which produce this error.