什么是使用静态局部变量的时候,我们可以在同样的成本获得一个全局变量? [英] What is the use of Static local variable when we can get a global variable at the same cost?

查看:174
本文介绍了什么是使用静态局部变量的时候,我们可以在同样的成本获得一个全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,有什么用静态存储类,当外部变量可以在相同的成本即达到其目的。无论是在可执行文件的数据段占用内存。

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 memory in the data segment of the executable.

我有外部variable.If更好的范围内,我想外部变量的范围是特定的文件,我不声明这个变量别人where.i看到很多的灵活性,全局变量静态局部变量

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

我们可以参照当地静态变量函数外,如果我们有variable.Memory的局部静态变量的地址将数据段不是在function.So独特功能的堆栈帧不静态存储类带来表。

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.

推荐答案

您写一个全局变量有一个更好的范围。这是不正确。它有一个更大的范围。更大的是不是更好。

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.

在我们的开发和维护复杂的软件能力的显著限制是人为错误。很多编程语言语义的限额语言为prevent错误。随着原始计算机,您可以添加两个指针,垃圾的堆栈指针,意外地浮动的字节加载到一个整数寄存器,等等。良好的编程语言,使这些错误很难通过错误的事情。

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.

这篇关于什么是使用静态局部变量的时候,我们可以在同样的成本获得一个全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆