Reputation: 31
Maybe this topic has been beaten to death but I can't find any "conclusive" answers. Anyway, I need to specify a plain old data only struct in C++17 that will be read/written to/from an external memory as a "raw chunk of bytes". This external memory content must specify it's memory layout "forever", regardless of any changing compiler settings, new compiler brands or versions, today and decades from now.
I am currently using the C++ **alignas **keyword and fixed width data types for every struct member but is this really the best approach? Any suggestions and gotchas would be greately appreciated.
FYI, this is an embedded project on an STM32H7xx MCU using ST CubeIDE.
Upvotes: 3
Views: 113
Reputation: 6074
There are many pitfalls of using fixed width data.
Being limited to 512 bytes is tricky. If you feel you can represent the data as reasonably portable data (32 or 64 bit int, float, double), then I'd use base64 encoding of your information in a sequence you choose and compress using zstd (best) or gzip (easy).
This should get you about 486 bytes of your data store in the 512 byte location and weather most compiler changes and other issues.
Upvotes: 1