<div dir="ltr"><div>Sizeof for a struct is going to be implementation dependent. The C standard leaves to the compiler to define padding to avoid alignment issues. Padding will be added when one of the members is followed by another member with a larger size or at the end of the struct.</div><div><br></div><div>What problem are you really trying to solve, though?<br></div><div><br></div><div>Also, just nit-picking, in the future please share your actual code so that we can run it through analyzers or check it locally.<br></div><div><br></div><div>-C<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Dec 13, 2019 at 12:25 AM Rick Hornsby <<a href="mailto:richardjhornsby@gmail.com">richardjhornsby@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I can do higher level languages like Python/Ruby “scripts”, but when<br>
it comes to C/C++ it seems like a different world. Probably because<br>
I’ve spent so little time in that world, especially over the last<br>
several years.<br>
<br>
This is an Arduino/ESP32 project, but the trouble I’m having is with<br>
my C++. I’ve been trying for a few days to understand what’s happening<br>
in this example -<br>
<br>
<a href="https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/examples/Prefs2Struct/Prefs2Struct.ino" rel="noreferrer" target="_blank">https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/examples/Prefs2Struct/Prefs2Struct.ino</a><br>
<br>
I understand enough of it to know that I'm trying to do something<br>
similar with an ESP32 microcontroller project, where when a command<br>
comes over the network to change a setting (that part is done and<br>
working), that setting is persisted to EEPROM (“preferences”) so that<br>
when power is lost, it will resume what it was doing when it starts<br>
back up. My struct is similar to the example:<br>
<br>
typedef struct {<br>
u8_t mode;<br>
u16_t activation_threshold;<br>
u8_t scheme_in_use;<br>
// u8_t custom_scheme[9]; // 3*RGB, could probably be 3*24bit (32) ints.<br>
} settings_t;<br>
<br>
One difference is that ‘activation_threshold’ in mine is a 16 bit int,<br>
whereas the example they’re all 8 bits.<br>
<br>
This creates a really, really weird thing I can’t figure out to where<br>
the sizeof() the struct is 1 byte for every 8 bit value, but if<br>
there’s a 16 bit value, there are 2 extra bytes. For example, if we<br>
ignore the 9 byte array ‘custom_scheme’ - we should have a 4 byte<br>
struct. For whatever reason, it’s 6. If I split activation_threshold<br>
into two 8 bit ints, the size of the struct is 4 bytes. Playing around<br>
with the struct, making one 32bit, moving that 32 bit to a different<br>
position in the struct, etc is coming up with different sizes. I<br>
suspect there’s some kind of memory allocation thing happening that I<br>
don’t understand.<br>
<br>
Either way. The more pressing but related thing is that I’m struggling<br>
to figure out how to serialize the struct into a byte array (?), and<br>
un-serialize it back from a byte array into a struct. This terrible<br>
looking monstrosity is all I’ve been able to figure out for the<br>
retrieval part:<br>
<br>
size_t settingsLen = prefs.getBytesLength("settings");<br>
char buf[settingsLen];<br>
prefs.getBytes("settings", buf, settingsLen);<br>
settings.mode = buf[0];<br>
settings.activation_threshold = (buf[1] << 8) | (buf[2] & 0xff);<br>
settings.scheme_in_use = buf[3];<br>
settings.custom_scheme[0] = buf[4];<br>
// …and so on.<br>
<br>
<br>
There’s no way the code should be this primitive … or better put,<br>
dumb? But if I try to follow the example more closely, using the cast<br>
like he does on line 29 of the example, the 16 bit value and<br>
everything after it is all screwy. It doesn’t seem to be as simple as<br>
the bytes go back into the slots of memory where they should?<br>
<br>
Any ideas?<br>
<br>
Thanks!<br>
<br>
_______________________________________________<br>
colug-432 mailing list<br>
<a href="mailto:colug-432@colug.net" target="_blank">colug-432@colug.net</a><br>
<a href="http://lists.colug.net/mailman/listinfo/colug-432" rel="noreferrer" target="_blank">http://lists.colug.net/mailman/listinfo/colug-432</a><br>
</blockquote></div>