5 ms·
This just saves you a single line of code compared to struct baz tmp = {.bar=1}; foo(&tmp);
by pantalaimon 4y ago
This just saves you a single line of code compared to
struct baz tmp = {.bar=1};
foo(&tmp);
- flohofwoe 4y agoThe point is that foo() now needs to check every struct item for being zero and use the default value instead. If the default values would be listed in the struct declaration, the compiler could fill those in when tmp is created, without any additional runtime overhead. It would also free up zero as being an actual value instead of standing for 'default value'.
- pantalaimon 4y agoThen just make a define with the default values #define FOO_INIT_DEFAULT {.bar=1} I don’t want some 'magic' struct that behaves different than all other structs on init. A static (default zero) struct lives in .bss, that entire section is zeroed on init. If you want to have default values, it goes to .data where it will consume some ROM.
- flohofwoe 4y agoIf you want your structs to be zero initialized, then you just wouldn't declare default values in the struct declaration, and everything would work as before. Also, once you initialize structs with values somewhere in the code (for instance with designated init), there's a high chance that the compiler will put a copy into the data section anyway, which is then memcpy'ed into the runtime struct (it depends on the compiler and compile options).