I’ve been reading up on C++ on the Internet, and here’s one thing that I haven’t been quite able to find an answer to.
I know that static variables used within functions are akin to globals, and that subsequent invocations of that function will have the static variable retain its value between calls.
However, if the function is never called, does the static variable get allocated?
Thanks
If the function is never called, it is likely that your linker will deadstrip both the function and the static variable, preventing it from entering
.rodata,.data, or.bsssegments (or your executable file format’s equivalents).However, there are various reasons why a linker might not deadstrip (flags telling it not to, an inability to determine what depends on the symbol, etc).
It’s worth checking your linker map file (sometimes just a text file!), or using
objdump,nm, ordumpbinutilities on the final executable to see if the symbol or related symbols (such as static initializer code) survived.