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

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

    In fact, a variable is tuple (storage, scope, type, address, value):

    storage     :   where is it stored, for example data, stack, heap...
    scope       :   who can see us, for example global, local...
    type        :   what is our type, for example int, int*...
    address     :   where are we located
    value       :   what is our value
    

    Local scope could mean local to either the translational unit (source file), the function or the block depending on where its defined. To make variable visible to more than one function, it definitely has to be in either DATA or the BSS area (depending on whether its initialized explicitly or not, respectively). Its then scoped accordingly to either all function(s) or function(s) within source file.

提交回复
热议问题