Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to declare multiple static variables with same name in a single C file?

Tags:

c

static

Is it possible to declare multiple static variables of same name in a single C file with different scopes? I wrote a simple programme to check this and in gcc it got compiled and worked fine.

Code:

static int sVar = 44;

void myPrint2()
{
    printf("sVar = %d\n", sVar++);
}

void myPrint()
{
    static int sVar =88;
    printf("sVar = %d\n", sVar++);
}

int main(void)
{
    static int sVar = 55;
    int i = 0;
    for (i = 0; i < 5; i++)
        myPrint();      
    printf("sVar = %d\n", sVar);
    myPrint2();
    return(0);
}

Now my question is since all "static" variable will reside in same section (.data) then how we can have multiple variables with same name in one section?

I used objdump to check the different section and found that all the static variables (sVar) were in .data section but with different names:

0804960c l     O .data  00000004              sVar
08049610 l     O .data  00000004              sVar.1785
08049614 l     O .data  00000004              sVar.1792

Why is the compiler is changing the name of the variables (since C doesn't support name mangling)?

like image 702
Mohammed Khalid Kherani Avatar asked May 28 '10 12:05

Mohammed Khalid Kherani


People also ask

Can we declare static variables multiple times?

Static variables can be assigned as many times as you wish. static int count = 0; is initialization and that happens only once, no matter how many times you call demo .

Can static members have multiple copies?

When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.

How many copies of a static variable can we have?

There is only one copy of a static variable or method for the whole class. For example, the main method is static because there should only be 1 main method. Static methods can be public or private.

Can static variable be initialized twice?

But in some cases it generates code that uses two guard variables and initialize each static variable separately. The problem is when such two types of initialization are mixed in executable binary. In such case it may happen that second static variable will get initialized twice.


2 Answers

A function-local static variable is something different than a global static variable.
Since there can be as many function-local statics with the same name as you like (provided they are all in different scopes), the compiler might have to change their names internally (incorporating the function's name or the line number or whatever), so that the linker can tell them apart.

like image 52
sbi Avatar answered Sep 20 '22 08:09

sbi


There is a difference between visibility and extent; also, the static keyword has different meanings based on scope.

Adding the static keyword to the block scope versions of sVar (myPrint::sVar and main::sVar) changes their extent (lifetime), but not their visibility; both are visible within their respective functions only, and will shadow or hide the file scope version within their local scope. It indicates that the variables have static extent, meaning the memory for them is allocated at program start and held until the program terminates (as opposed to automatic or local extent, where their lifetime is limited to the scope in which they are defined).

Adding the static keyword to the file scope version of sVar doesn't change its extent (file scope variables have static extent by definition), but it does change its visibility with respect to other translation units; the name is not exported to the linker, so it cannot be accessed by name from another translation unit. It is still visible within the current translation unit, which is why myPrint2 can access it.

like image 20
John Bode Avatar answered Sep 22 '22 08:09

John Bode