C Global and Static

You might also like

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 3

From what I have learned about global and static variables, If a C variable is declared

outside all functions in a source file as:


int a;

This variable can be accessed by other files once it has an extern declaration for it in that
file.
But if the same variable is declared as:
static int a;

then this variable can be used only by the current file, any other file wont be able to see
this variable.
1. When the program is loaded into the memory at run time, both Global and static
variable are present in the Data section of this program.
I want to understand that as both are stored in the same memory segment, how is
the static variable protected from not getting used in any instruction out of its
scope.
What I think is that the scope of the variable and its access will be taken care of by
the compiler. Please comment if I am wrong and add if I am missing any detail.
2. Regarding Extern variable. If,
int a;

is defined in file file1.c and is declared in file file2.c as:


extern int a;

both files belongs to different processes, let it be process1 and process2


respectively. So when process1 is running and its address space is loaded in the
memory its data section variable "a" will be available.
I have a doubt here, that is, when process2 is running will this variable also be
loaded in process2's data section? or how it is managed.
Please help me clear my above mentioned doubts. I have searched on the web and read a
few articles and need to confirm if I have understood is correctly.
Aso, please do suggest me a good article or book which will help me understand the
above concepts clearly.
c
edited Jun 6 '13 at 9:30 asked Jun 6 '13 at 7:30
share|improve this question

Suzuka user2333014
249 7913

add a
comment

4 Answers
active oldest votes
First of all, different processes have different address spaces, as you said. So, unless you
share them explicitly (shared memory or the like) they do not share any memory.
About the global static vs. global non-static variables, the difference is called linkage: nonstatic global variables have external linkage, meaning that they have a name for the linker,
and so one compilation unit (.c file) can access the variable defined in another.
up vote 9
A static global variable, however, has internal linkage, and so, although it may be in the
down
same memory block than the former, it has no name for the linker, and so it cannot be used
vote
from any other compilation unit than its own.
answered Jun 6 '13 at 7:38
share|improve this answer

edited Jun 6 '13 at 12:50


rodrigo
34.2k22560

add a comment

1. Static variables get assigned an address and a size in the memory, but they do not get
advertized. So if I have a static int a; in one file and try to refer to it with
extern int a; from another file, the "link end" cannot be found, so it just
doesn't work.

up vote 2
down
vote

In order to make extern work, there has to be "something" which advertizes a as


available, what static does not do.
2. No, they don't belong to different processes. They are linked together into one
executable file which then gets executed. Different processes normally cannot acces
each other's memory.
answered Jun 6 '13 at 7:36
share|improve this answer

glglgl
36.3k33969

add a comment
up vote 1 Having a static variable at the file scope makes it invisible to the linker at linking time.
down

The compiler doesn't issue a directive that a symbol (named correspondingly to the
variable) should be visible to the linker.

vote

Regarding the second question, extern int a is just a declaration. It doesn't reserve
any space for that variable, but merely informs the compiler that such a variable exists
somewhere. When the unit is later linked, the reference to that variable is resolved by the
linker.
answered Jun 6 '13 at 7:36
share|improve this answer

Blagovest Buyukliev
23.1k64181

add a comment
1 What I think is that the scope of the variable and its access will be taken care of by
the compiler. Please comment if I am wrong and add if I am missing any detail.
That is correct. The C compiler doesn't make this variable accessible to other compilation unit, so
you can't successfully compile a program that directly accesses that variable. C calls this concept
linkage.
You could write a non-static function that return its address to get indirect access to it:
static int a;

up vote
1 down
vote

int *get_a(void)
{
return &a;
}Now other compilation units could indirectly access a by calling get_a()Other
than that, it's up to a particular compiler how this is represented during
runtime. If you can somehow, outside C, figure out the location of a, you can
mess with it as much as you want. There's normally no special protection or the
like.

2 Regarding Extern variable. If when process2 is running will this variable also be
loaded in process2's data section? or how it is managed.
Normal destktop/server operating systems provide virtual memory to each process. Memory wise,
these processes are independent of each other and process2 sees its own copy of the a variable,
which doesn't have anything to do with the copy of a in process1. The only common thing is
they're loaded from the same executable file.

You might also like