I just came across this good question at SO: Why are global and static variables initialized to their default values? But I don't like any of the answers and I actually think it remains unanswered: why does C and C++ perform zero initialization of static storage duration variables?
C++ surely does it because C does it. And C likely does it simply because that's how ancient 1970s Unix did it, as specified by ABI or executable formats? The Wikipedia about .bss
suggests that the origin of the name goes even further back to pre-Unix mainframe computers, but not necessarily as a zero-initialized section until Unix, or...?
At the same time, C does not perform zero initialization of stack or heap memory (automatic or allocated storage). The reason why is supposedly performance, which is also one reason why malloc
has not been declared obsolete in favor of calloc
.
But can't the same be said about zero initialization of .bss
? Doesn't this lead to slower executable boot-up and if we skipped it we would get faster programs? Yet it has been there from the start.
As someone who usually works with embedded systems microcontrollers, I know that it is very common that the CRT comes with an option "fast boot-up" or "non-standard boot-up" skipping .bss
and .data
initialization in explicit violation of the C standard, just in order to start up faster. Hosted/PC systems don't really have that option but always perform .bss
initialization - supposedly inexpensive but it is still overhead.
Does anyone know why .bss
zero initialization was introduced and made mandatory when Unix and C were created?