8

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.

0

5 Answers 5

12

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: non-static 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.

A static global variable, however, has internal linkage, and so, although it may be in the same memory block than the former, it has no name for the linker, and so it cannot be used from any other compilation unit than its own.

2
  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.

    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.

1

Having a static variable at the file scope makes it invisible to the linker at linking time. The compiler doesn't issue a directive that a symbol (named correspondingly to the variable) should be visible to the linker.

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.

1

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;

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.

0

both files belongs to different processes means that the compiler has absolutely no reference for what extern int a; refers to. It doesn't search through every piece of code on your computer and hope it finds one and only one int a; declaration and then add in a bunch of programming to detect and access that process's memory. Besides being terrible behavior, the amount of complexity would be absurd with no actual guarantee of it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.