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

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

    you already know either it store in bss(block start by symbol) also referred as uninitialized data segment or in initialized data segment.

    lets take an simple example

    void main(void)
    {
    static int i;
    }
    

    the above static variable is not initialized , so it goes to uninitialized data segment(bss).

    void main(void)
    {
    static int i=10;
    }
    

    and of course it initialized by 10 so it goes to initialized data segment.

提交回复
热议问题