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

前端 未结 16 2183
自闭症患者
自闭症患者 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:38

    static variable stored in data segment or code segment as mentioned before.
    You can be sure that it will not be allocated on stack or heap.
    There is no risk for collision since static keyword define the scope of the variable to be a file or function, in case of collision there is a compiler/linker to warn you about.
    A nice example

提交回复
热议问题