Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Global and Static variable storing in memory

Tags:

c

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.

like image 237
user2333014 Avatar asked Jun 06 '13 07:06

user2333014


People also ask

Where static and global variables are stored in memory?

Global and static variables are stored in the address space of a virtual processor, in the data segment of a shared-object file.

Are global variables stored in memory?

Global variables are stored in the data section. Unlike the stack, the data region does not grow or shrink — storage space for globals persists for the entire run of the program. Finally, the heap portion of memory is the part of a program's address space associated with dynamic memory allocation.

How are static variables stored in memory C?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).

Are static variables stored on the stack in C?

2) Static variables are allocated memory in data segment, not stack segment.


1 Answers

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.

like image 133
rodrigo Avatar answered Sep 29 '22 16:09

rodrigo