Where are static variables stored in C and C++?

前端 未结 16 2171
自闭症患者
自闭症患者 2020-11-22 02:00

In what segment (.BSS, .DATA, other) of an executable file are static variables stored so that they don\'t have name collision? For example:


foo.c:                  


        
16条回答
  •  失恋的感觉
    2020-11-22 02:24

    Data declared in a compilation unit will go into the .BSS or the .Data of that files output. Initialised data in BSS, uninitalised in DATA.

    The difference between static and global data comes in the inclusion of symbol information in the file. Compilers tend to include the symbol information but only mark the global information as such.

    The linker respects this information. The symbol information for the static variables is either discarded or mangled so that static variables can still be referenced in some way (with debug or symbol options). In neither case can the compilation units gets affected as the linker resolves local references first.

提交回复
热议问题