Difference between 'global' and 'stati

2019-01-21 05:14发布

问题:

A global variable's scope is in all the files, while a static global variable's scope is just the file where it is declared. Why so?

Where are global or static global variables stored in memory?

回答1:

There is some confusion, since static in C can mean two different things. One is static storage duration, and the other is internal linkage. static used as a keyword on file-scope will give the function or object it is used with internal linkage.

Internal linkage for a function or object means that if you declare another function in another "file" (this is not really called "file", but rather translation unit - TU), then that declaration will refer to a different function: The name declared in that unit will "link" to a different entity than the name declared in that other translation unit, which was "internal" to that TU. The same applies to objects.

Whether or not a file-scope variable is declared with static, it will still have a static storage duration: That means it lives through the whole program, and dies when the program terminates. Another example of an object that has static storage duration is a string literal. Where objects that have static storage duration are stored isn't specified, but they are usually stored depending on whether they are initialized or not: Initialized file-scope variables are usually stored in a section called ".data", while non-initialized file-scope variables are usually stored in a section called ".bss". Remember that if the variable isn't initialized, it will be zero initialized at the start of the program: The ".bss" section is usually zero initialized by an implementation on program's startup.

I said "usually" everywhere, since where things are stored isn't specified. For example, some implementations could store string literals in a read-only section. And if you have a file-scope pointer and don't initialize it, the implementation initializes it to a null-pointer, which is not necessarily an object with all null bytes :)



回答2:

They're both stored in the data segment; the difference is that the global has an externally-visible linker symbol, and the static global does not.



回答3:

A global variable's scope is in all the files.. while a static global variable's scope is just the file where it is declared.. why so ?

A global variable is intended to be accessible from any module - this practice it something considered poor practice, and should be used only if absolutely necessary.

File scoped static variables (what I assume you're talking about when you say "static global") can be accessed by the routines in a single compilation unit (generally a file) - the reason for this is to limit it's scope. When modifying code that uses the variable, there's a nice limit to where you need to look to see what other routines might be affected. It also reduces the opportunity for there to be a name clash.

When using global variables, if another set of modules happens to also use a global variable with the same name for a different purpose you'll have to modify one set to to use a different name. That problem doesn't exist for static variables.



回答4:

Static globals cannot be accessed from other files while global variables can be accessed using the extern keyword.



回答5:

The C Standard doesn't specify where they are stored in memory, and it is be of no concern to the C programmer. The ability to access the variables is controlled by the compiler and the linker.

This is the second question you have asked on this topic area. One of the main reasons for using a relatively high-level programming language like C is that you don't have to worry about these issues.



回答6:

We use static attribute to hide variable and function declarations inside modules, much as we would use public and private declarations in Java and C++. C source files play the role of modules. Any global variable or function declared with the static attribute is private to that module. Similarly, any global variable or function declared without the static attribute is public and can be accesses by any other module. Though it is a good practice to define as variables or functions with the 'global' keyword.

Both these variables are stored in .data or .bss section of a relocatable file.



回答7:

Points to Remember to Crack Interviews Questions on Static and Global

  1. The memory allocated for static variables is only once i.e all the objects shared by same memory but when it comes to global variables memory is allocated for each and every object.
  2. Static and global variable differ in life and scope. First, let me explain what is life and scope. Life of an object determines whether the object is still in the memory whereas the scope of the object is whether it can access the variable by its name.a) It is possible that object is live, but not visible (not in scope) b)If an object is not alive but in scope (except for dynamically allocated objects where you refer object through pointers).
  3. The global variable has global scope, I mean it can be accessed by any function, from any file, whereas static variable has file scope, it is not possible to access the variable from any other file. This technique is helpful when u want to make, variable accessible to all functions of that file, but not functions of another file.