Home >Backend Development >C++ >Where Are Static Variables Stored in C and C Executable Files?
Static variables are a unique type of variable in C and C , which are stored in specific segments of executable files to avoid name collisions. In the provided code example, despite having the same name "foo" and "bar" in different translation units, they function independently with their incremented values. This independence raises the question: where are these static variables stored in the executable file?
To understand the storage location, it's crucial to consider whether the static variables are zero-initialized. If they are, they are typically placed in the ".BSS" (Block Started by Symbol) segment. This segment stores uninitialized data that is set to zero by default. For example, "foo = 1;" would be stored in ".DATA".
Meanwhile, non-zero-initialized static variables are placed in the ".DATA" segment. This segment contains initialized data, such as "foo = 10;". The separation into these segments ensures no name collisions and allows for efficient memory allocation.
The above is the detailed content of Where Are Static Variables Stored in C and C Executable Files?. For more information, please follow other related articles on the PHP Chinese website!