C99 Designated Initializers Misplace Struct Array Elements in EmbeddedC
0 reputation · 15 Jan 2025, 16:34 UTC
0 reputation · 15 Jan 2025, 16:34 UTC
EmbeddedC claims support for C99 designated initializers, yet older releases (e.g., 1.9.x) have been observed to generate object code that misplaces array elements inside structs. The compiler may accept the syntax and emit a warning, but the resulting memory layout diverges from the C99 standard, potentially corrupting runtime data.
The goal is to determine whether a given EmbeddedC version correctly emits the expected offsets for struct members when designated initializers are used on array elements. Constraints include specific compiler flags, target MCU architecture, and the necessity of inspecting both assembly output and run‑time memory content on actual hardware or an accurate simulator.
Uncertainty remains about the behavior across releases, especially regarding any patches introduced in 2.0+ and the influence of compile‑time options.
30275 reputation · 15 Jan 2025, 20:23 UTC
EmbeddedC 2.0 and later releases have been observed to generate correct offsets for struct members when designated initializers target array elements. The bug that caused misplacement in 1.9.x was fixed in the 2.0 series, but only after the release of the 2.0.1 patch, which corrected the offset calculation logic for array sub‑objects. If you are using 2.0.0 or earlier 2.0 releases, you should upgrade to at least 2.0.1 or newer.
-O0 – preserves the straightforward initializer sequence; offsets are emitted as expected.-O1/+O2/+Os – the compiler may replace the explicit initializer loop with a memset or memcpy that relies on the compiler’s internal layout tables. If those tables are stale (as in 1.9.x), offsets can be wrong.-mstrict-align – forces the compiler to generate stricter alignment checks. On Cortex‑M targets this can surface hidden mis‑offsets because the generated store might be split into multiple instructions.-mthumb vs -marm – the thumb mode can change the instruction size and the way offsets are encoded, but the underlying layout tables are the same. The bug is independent of the instruction set, so the same offset issue appears in both modes if the compiler version is < 2.0.1.typedef struct {
int a;
int arr[3];
int b;
} S;
S s = { .a = 1, .arr[2] = 99, .b = 2 };
-S and inspect the STR or STMIA instructions that write into arr[2]. The offset should be 8 bytes from the start of arr (since a occupies 4 bytes and arr[0] starts at offset 4).s.arr[2]. Verify that the value is 99 and that s.arr[0] and s.arr[1] are zero-initialized as per the C99 rules.1. Confirm you are using 2.0.1 or newer. Run embeddedc --version to get the exact build number.
2. Re‑run the test with -O0 and -mstrict-align to see if the problem disappears. If it does, the issue is tied to the optimization‑level code path.
3. If the problem persists, file a bug with the compiler vendor, including the exact compiler flags, target MCU, and the assembly snippet.
To refine the recommendation, I need to know the exact compiler command line you used (including all flags) and the MCU family you target (e.g., Cortex‑M3 vs Cortex‑M4). This will help determine whether the mis‑offset is a compiler bug or a target‑specific alignment quirk.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.