-->

What is the use of Static local variable when we c

2019-01-22 10:28发布

问题:

In C ,what is the use of static storage class when an external variable can serve its purpose at the same cost ie. both occupy storage space in the data segment of the executable.

I have much better scope with external variable.If i want the scope of external variable to be specific file i do not declare this variable else where.i see a lot of flexibility with a global variable that static local variable

And we can refer to local static variable outside the function if we have the address of the variable.Memory for local static variable will be in Data segment not in the stack frame of the function.So unique feature does static storage class bring to the table.

I just want to know whether static has any subtle purpose that i m not aware of.

回答1:

You write that a global variable has a “better” scope. This is incorrect. It has a bigger scope. Bigger is not better.

Bigger may be necessary, if you need an identifier to be visible in more places, but this is often not the case. But a bigger scope means more exposure to errors. Global variables muddle the semantics of routines by making it harder to see what program state they use and change, and it increases the probability of errors caused by failing to declare a local identifier and of other errors.

In particular, an identifier with external linkage will collide with identifiers in other libraries. Consider what happens when you are writing a physics application, have an external identifier named acceleration, and link with a physics library that also has an external identifier named acceleration. The program will fail. Because of this, external identifiers are usually bad design.

A significant limit on our ability to develop and maintain complex software is human error. Much of programming language semantics limits the language to prevent errors. With a raw computer, you can add two pointers, trash your stack pointer, accidentally load the bytes of a float into an integer register, and so on. Good programming languages make these errors difficult to do by mistake.

Global variables were a larger source of errors before scoping rules helped control them. Good programmers limit the scopes of their identifiers.



回答2:

A global variable is well, global, it can be accessed from anywhere.

A static local variable has local scope. It is static, so it's lifetime runs across the lifetime of the application however it can only be accessed from the local scope (whether that scope is a function, a block, or a file)



回答3:

The basic difference is on scope of variable.

1) global variable is global for entire project. lets say your project has 10 different files then all 10 files can access the global variable(see how to use extern).

2) static variable/function can be used by function/file within which it is defined. It cannot be used by any other file in your project.

yet, you can modify the static variable(defined in func1()) in func2() by passing reference of the variable. please look into below example,

void func2(int *i)
{
    (*i)++;
}

void func1()
{
    static int i;

    i=1;
    printf("%d\n", i);
    func2(&i);
    printf("%d\n", i);  
}

int main()
{
    func1();
    return 0;
}

As you see above, func1() has static int i which cannot be directly manipulated by func2(), but if you pass reference of the variable you can still manipulate the variable like ordinary variable.

hope it helps...



回答4:

Difference between local and global first and foremost is the scope: you can access local variables only from within the block they're defined in, while global variables can be accessed from anywhere. Consequently, you can only have one variable with a given name in global scope, but you can have multiple local static variables in different functions.

As with static global variables versus extern variables: yes, static global variables are local to the translation unit (i.e. the .c source file they're defined in).

So the main concern here is the notion of scope, and the storage comes naturally from there.



回答5:

The reason you should use a local static variable is scope, and therefore avoiding some bug prone situations since using a local static variable you'll not be able to refer to it outside the function it was defined in.



回答6:

Here's a short program which demonstrates the difference:

#include <stdio.h>

static int a=20;
void local()
{
   printf("%d,addr:%d \n", a, (void*)&a);
   a = 100;
}

int main()
{

      {
         static int a = 10;
         printf("%d,addr:%d \n", a, (void*)&a);
         local();
      }
      printf("%d addr:%d \n", a, (void*)&a);
}

Output:

10,addr:134518604   -- local static inside the braces
20,addr:134518600   -- this is referring the global static variable
100 addr:134518600  -- This is referring the global static variable which is outside of 
                       the braces.

Here braces also matters: if no braces in the main() function then it refers local static variable only.