Is it possible to declare multiple static variable

2020-01-29 07:24发布

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)?

标签: c static
6条回答
Summer. ? 凉城
2楼-- · 2020-01-29 07:37

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.

查看更多
霸刀☆藐视天下
3楼-- · 2020-01-29 07:37

The static keyword has different meanings when it's global and local, the first sVar is just a global variable unavailable to other translation units. And static variables sVar in myPrint() and main() are in different scopes, so they are different variables. In the body of myPrint() sVar refers to the local static (hiding global sVar), in myPrint2() it refers to the global (it's not hidden by anything local), and in main() it refers to the local static sVar, which again hides the global since the moment it was declared.

查看更多
混吃等死
4楼-- · 2020-01-29 07:38

Both function will create different stacks, so static part also will be different .Therefore it is possible.

查看更多
一夜七次
5楼-- · 2020-01-29 07:49

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.

查看更多
何必那么认真
6楼-- · 2020-01-29 07:57

Your first sVar is global to file, second one is local to the function myPrint() and third one is local to main()

Function level static variables are not accessible outside the function - they just retain their values on subsequent calls. I assume you've already noted this when you ran your code.

查看更多
Melony?
7楼-- · 2020-01-29 08:01

Block scoping. Read K & R.

查看更多
登录 后发表回答