Page 1 of 1

ESP32-S3 and 32 Bit data alignment

Posted: Thu Sep 14, 2023 11:54 am
by greg-dickson
Could someone please enlighten me.

Does the ESP32-S3 and other ESP32 chips align data on 32bit words.

In other words.

Does a unint32_t and a uint8_t take the same amount of memory space?

Thank you.

Re: ESP32-S3 and 32 Bit data alignment

Posted: Thu Sep 14, 2023 12:46 pm
by MicroController
No, the chips don't align any data. The compiler does.
The flash and DRAM are byte-addressable/-accessible in hardware, hence gcc (usually) does 1-byte alignment on 8-bit values.
You can check the value of sizeof(uint8_t) or sizeof(uint32_t) yourself.
However,

Code: Select all

struct {
  uint8_t u8;
  uint32_t u32;
} x;
will occupy 8 bytes due to gcc's (on ESPs mostly unnecessary) padding/alignment of the uint32_t.

Re: ESP32-S3 and 32 Bit data alignment

Posted: Thu Sep 14, 2023 2:07 pm
by greg-dickson
MicroController wrote:
Thu Sep 14, 2023 12:46 pm
No, the chips don't align any data. The compiler does.
The flash and DRAM are byte-addressable/-accessible in hardware, hence gcc (usually) does 1-byte alignment on 8-bit values.
You can check the value of sizeof(uint8_t) or sizeof(uint32_t) yourself.
However,

Code: Select all

struct {
  uint8_t u8;
  uint32_t u32;
} x;
will occupy 8 bytes due to gcc's (on ESPs mostly unnecessary) padding/alignment of the uint32_t.
Thanks I did check another way by adding an array to some code and checking the upload size and it did follow your explanation.
Thank you I couldn't find any specific information elsewhere.