当我们以相同的代价获得全局变量时,静态局部变量的用途是什么?

时间:2021-06-20 13:37:48

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.

在C中,当外部变量以相同的成本服务于其目的时,静态存储类的用途是什么。两者都占用了可执行文件数据段中的存储空间。

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

我有更好的外部变量范围。如果我希望外部变量的范围是特定文件,我不要声明这个变量else where.i看到很多灵活性与全局变量静态局部变量

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.

我只是想知道静态是否有任何我不知道的微妙目的。

6 个解决方案

#1


24  

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.

特别是,具有外部链接的标识符将与其他库中的标识符冲突。考虑在编写物理应用程序时会发生什么,拥有一个名为acceleration的外部标识符,并链接到一个物理库,该物理库还有一个名为acceleration的外部标识符。该计划将失败。因此,外部标识符通常是糟糕的设计。

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


6  

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


1  

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

1)全局变量是整个项目的全局变量。假设您的项目有10个不同的文件,那么所有10个文件都可以访问全局变量(请参阅如何使用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.

2)静态变量/函数可以由定义它的函数/文件使用。它不能被项目中的任何其他文件使用。

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

但是,您可以通过传递变量的引用来修改func2()中的静态变量(在func1()中定义)。请看下面的例子,

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.

如上所示,func1()具有静态int i,它不能被func2()直接操作,但是如果传递变量的引用,你仍然可以像普通变量一样操纵变量。

hope it helps...

希望能帮助到你...

#4


0  

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

与静态全局变量和外部变量一样:是的,静态全局变量是转换单元的本地变量(即它们定义的.c源文件)。

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

因此,主要关注的是范围的概念,存储自然而然地存在。

#5


0  

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


-1  

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.

大括号也很重要:如果main()函数中没有大括号,那么它只引用局部静态变量。

#1


24  

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.

特别是,具有外部链接的标识符将与其他库中的标识符冲突。考虑在编写物理应用程序时会发生什么,拥有一个名为acceleration的外部标识符,并链接到一个物理库,该物理库还有一个名为acceleration的外部标识符。该计划将失败。因此,外部标识符通常是糟糕的设计。

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


6  

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


1  

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

1)全局变量是整个项目的全局变量。假设您的项目有10个不同的文件,那么所有10个文件都可以访问全局变量(请参阅如何使用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.

2)静态变量/函数可以由定义它的函数/文件使用。它不能被项目中的任何其他文件使用。

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

但是,您可以通过传递变量的引用来修改func2()中的静态变量(在func1()中定义)。请看下面的例子,

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.

如上所示,func1()具有静态int i,它不能被func2()直接操作,但是如果传递变量的引用,你仍然可以像普通变量一样操纵变量。

hope it helps...

希望能帮助到你...

#4


0  

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

与静态全局变量和外部变量一样:是的,静态全局变量是转换单元的本地变量(即它们定义的.c源文件)。

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

因此,主要关注的是范围的概念,存储自然而然地存在。

#5


0  

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


-1  

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.

大括号也很重要:如果main()函数中没有大括号,那么它只引用局部静态变量。