Hughe Stack Usage when having a long Initializer List
Christoph Rüthing
christoph@muppetnet.net
Wed Nov 5 11:33:23 GMT 2025
Hi,
today I encountered an interesting behavior of GCC for a constructor with a very long initializer list, initializing ~1000 members of different plain types like int, float, ... I see, that GCC stores basically all addresses of the members on the stack and then reads back the addresses one by one to write the initializtion values.
I wonder why GCC behaves like this? For me this is useless stack usage, because it finally have pushed addresses x, x+4, x+8, ...
Note: I am using GCC for ARM version 10.3.1 but we can see the behavior also in newer versions. Check the example here https://godbolt.org/z/qn9xeqvbv and you see that the Test::Test() constructor allocates 940 bytes stack and it stores just increasing addresses on the stack.
Switching to -O1 or -O2 removes this behavior, I think some optimization tries to optimize the code here with the cost of quite big stack usage. Are there options that influence this behavior?
Best Regards,
Christpoh
More information about the Gcc-help
mailing list