凡在本地,全局,静态自动,注册,EXTERN,常量,volatile变量存储? [英] Where are the local, global, static, auto, register, extern, const, volatile variables are stored?

查看:121
本文介绍了凡在本地,全局,静态自动,注册,EXTERN,常量,volatile变量存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里是在本地,全局,静态自动,注册,EXTERN,常量,存储volatile变量?

Where are the local, global, static, auto, register, extern, const, volatile variables stored?

推荐答案


  • 本地变量可以在栈或数据段取决于他们是否是自动或静态存储。 (如果明确指定既不自动或静态的,假设自动)

  • local variables can be stored either on the stack or in a data segment depending on whether they are auto or static. (if neither auto or static is explicitly specified, auto is assumed)

    全球变量存储在一个数据段(除非编译器可以优化他们离开,见常量),并有从申报点的可视性,编译单元的结束。

    global variables are stored in a data segment (unless the compiler can optimize them away, see const) and have visibility from the point of declaration to the end of the compilation unit.

    静态变量存储在一个数据段(同样,除非编译器可以优化他们离开),并有从申报点到封闭范围的端到端可视性。这是不是静态的全局变量也可以看到在其他编译单元(见EXTERN)。

    static variables are stored in a data segment (again, unless the compiler can optimize them away) and have visibility from the point of declaration to the end of the enclosing scope. Global variables which are not static are also visible in other compilation units (see extern).

    自动变量是局部的,并存储在堆栈上。

    auto variables are always local and are stored on the stack.

    注册修饰符告诉编译器尽力保持变量在寄存器中,如果在所有可能的。否则,它被存储在堆栈上。

    the register modifier tells the compiler to do its best to keep the variable in a register if at all possible. Otherwise it is stored on the stack.

    的extern 变量存储在数据段。该extern修饰符告诉不同的编译单元实际上是声明变量,所以不要创建它的另一个实例还是会有在链接时名称冲突编译器。

    extern variables are stored in the data segment. The extern modifier tells the compiler that a different compilation unit is actually declaring the variable, so don't create another instance of it or there will be a name collision at link time.

    常量变量可以在堆栈或者取决于他们是否是自动或静态只读数据段进行存储。然而,如果编译器可以确定它们不能从一个不同的编译单元被引用,或者您code未使用const变量的地址,它是自由地优化它远(每个参考可由取代恒定值)。在这种情况下,它不存储。

    const variables can be stored either on the stack or a readonly data segment depending on whether they are auto or static. However, if the compiler can determine that they cannot be referenced from a different compilation unit, or that your code is not using the address of the const variable, it is free to optimize it away (each reference can be replaced by the constant value). In that case it's not stored anywhere.

    挥发性修改告诉一个变量的值可以在改变编译器随时从外部影响(通常是硬件),因此它不应该尝试从内存优化掉任何重载成当变量被引用注册。这意味着静态存储。

    the volatile modifier tells the compiler that the value of a variable may change at anytime from external influences (usually hardware) so it should not try to optimize away any reloads from memory into a register when that variable is referenced. This implies static storage.

    BTW这一切适用于C&放大器; C ++以及Objective-C的。

    BTW all this applies to C & C++ as well as Objective-C.

    这篇关于凡在本地,全局,静态自动,注册,EXTERN,常量,volatile变量存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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