I'm writing a program in C++/C for raspberry pi pico. Pico has a 2MB of flash memory and it's SDK provides a function which allows to write data to that memory:
void flash_range_program(uint32_t flash_offs, const uint8_t *data, size_t count)
Second parameter of that function is the data which we want to write in the memory. I want to save strings and floats in that memory, but I don't know what is the best (performance wise) way to convert std::string and float to __const uint8_t*__.
Assuming that
std::uint8_tisunsigned char(as is usually the case), you are allowed to simply access the object representation of afloatvariable calledfviaand the contents of a
std::stringvariable calledsviaThe corresponding sizes are
sizeof(f)ands.size()(ors.size()+1if you want to store the null terminator of the string as well).This is assuming of course that you want to store the object representation of the
floatvariable. If you want to store a serialized representation instead, then first convert to astd::stringin an appropriate format.In the unlikely scenario that
uint8_tis not an alias forunsigned charyou are not allowed to substituteunsigned charwithuint8_tin the shown casts. They would then cause aliasing violations and therefore undefined behavior when the values are accessed through the resulting pointers.