C语言 本地、全局、静态、自动、寄存器、外部、常量、易失性变量存储在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3684760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where are the local, global, static, auto, register, extern, const, volatile variables are stored?
提问by Matrix
Where are the local, global, static, auto, register, extern, const, volatile variables stored?
本地、全局、静态、自动、寄存器、外部、常量、易失性变量存储在哪里?
回答by Ferruccio
localvariables 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)
globalvariables 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.
staticvariables 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).
autovariables are always local and are stored on the stack.
the registermodifier 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.
externvariables 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.
constvariables 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 volatilemodifier 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.
局部变量可以存储在堆栈中,也可以存储在数据段中,具体取决于它们是自动的还是静态的。(如果没有明确指定 auto 或 static,则假定为 auto)
全局变量存储在数据段中(除非编译器可以优化它们,请参阅 const)并且从声明点到编译单元结束都具有可见性。
静态变量存储在数据段中(同样,除非编译器可以优化它们)并且从声明点到封闭范围的末尾都具有可见性。非静态的全局变量在其他编译单元中也是可见的(参见 extern)。
auto变量始终是本地的并存储在堆栈中。
该寄存器修饰符告诉编译器尽力保持变量在寄存器中,如果在所有可能的。否则,它存储在堆栈中。
extern变量存储在数据段中。extern 修饰符告诉编译器不同的编译单元实际上正在声明该变量,因此不要创建它的另一个实例,否则在链接时会发生名称冲突。
const变量可以存储在堆栈或只读数据段中,具体取决于它们是自动还是静态。但是,如果编译器可以确定不能从不同的编译单元引用它们,或者您的代码没有使用 const 变量的地址,则可以自由优化它(每个引用都可以替换为常量值) . 在这种情况下,它不会存储在任何地方。
的挥发性改性剂告诉编译器,一个变量的值可在任何时间从外部影响(通常的硬件)来改变,因此不应该尝试时变量被引用到从存储器优化掉任何重新加载到寄存器中。这意味着静态存储。
BTW all this applies to C & C++ as well as Objective-C.
顺便说一句,所有这些都适用于 C & C++ 以及 Objective-C。
回答by hotpaw2
At what level of abstraction are you looking for an answer?
您在什么抽象层次上寻找答案?
At the physical level, they're all probably stored in gate capacitances and magnetic domains. (Maybe even photons if your swap disk is wifi or optical fiber connected.)
在物理层面,它们可能都存储在栅极电容和磁域中。(如果您的交换磁盘是 wifi 或光纤连接,甚至可能是光子。)
At one hardware level, copies of any and all of these variables could exist at several places in the register, data cache (perhaps in multiple levels), main memory, and/or storage hierarchy, everything from completely swapped out to disk or NV storage (depending on the existence, implementation, and current state of any demand-paged virtual memory subsystem), to perhaps everything in registers if your apps size and lifetime is tiny enough.
在一个硬件级别,任何和所有这些变量的副本都可能存在于寄存器、数据缓存(可能在多个级别)、主内存和/或存储层次结构中的多个位置,从完全换出到磁盘或 NV 存储的所有内容(取决于任何按需分页虚拟内存子系统的存在、实现和当前状态),如果您的应用程序大小和生命周期足够小,则可能是寄存器中的所有内容。
Given the most familiar compiler and runtime implementations, memory (perhaps virtual) is chopped into things called stacks and heaps. Given the formal language definition, this chopping may or may not be required.
鉴于最熟悉的编译器和运行时实现,内存(可能是虚拟的)被分割成称为堆栈和堆的东西。鉴于正式的语言定义,可能需要也可能不需要这种切割。
At the compiler optimization level, many of these variable may have been optimized out of existence. They're not stored anywhere except as an abstraction.
在编译器优化级别,许多这些变量可能已被优化而不再存在。除了作为抽象之外,它们不会存储在任何地方。
回答by Ignacio Vazquez-Abrams
Local and autovariables are stored on the stack. Global and staticvariables are stored in a DATA page. registervariables are stored in a register on the CPU if possible, otherwise in the stack. extern, const, and volatiledo not specify where the variable is stored; the variable is stored where the other storage specifiers say they are.
局部auto变量和变量存储在堆栈中。全局和static变量存储在数据页中。register如果可能,变量存储在 CPU 上的寄存器中,否则存储在堆栈中。extern, const, 并且volatile不指定变量的存储位置;该变量存储在其他存储说明符所说的位置。
回答by mipadi
Local variables are usually stored on the stack, and global variables in a program's "text" segment (in the case of string constants) or on the heap if they're dynamically allocated. Auto variables are usually used in functions/methods, and are generally passed on the stack (sometimes in registers, too, depending on architecture). Register variables are were once stored in registers, but most compilers nowadays ignore the register keyword and put them wherever they see fit -- on the stack or in a register. Extern, const, and volatile members are modifiers and so have no definitive place where they are stored.
局部变量通常存储在堆栈中,全局变量存储在程序的“文本”段(在字符串常量的情况下)或堆中(如果它们是动态分配的)。自动变量通常在函数/方法中使用,并且通常在堆栈上传递(有时也在寄存器中,取决于体系结构)。寄存器变量曾经存储在寄存器中,但现在大多数编译器忽略了 register 关键字并将它们放在它们认为合适的任何地方——堆栈或寄存器中。Extern、const 和 volatile 成员是修饰符,因此没有明确的存储位置。
So the short answer is, as usual, "it depends".
因此,像往常一样,简短的回答是“视情况而定”。
回答by kumar123
LOCAL- Local variables which scope is with in the function.It may be two types auto or static. If it is declared simply int var.Compiler treat as auto storage class. The auto variables are stored in Stack. The static variables are stored in Data Segment.
LOCAL- 函数中作用域所在的局部变量。它可以是自动或静态两种类型。如果它被简单地声明为 int var.Compiler 视为自动存储类。自动变量存储在堆栈中。静态变量存储在数据段中。
The register variables are stored in CPU.If no registers are available to store variables.then compiler treat as auto variable.
寄存器变量存储在 CPU 中。如果没有寄存器可用于存储变量。则编译器将其视为自动变量。
The global variables are stored in Data Segment area.
全局变量存储在数据段区域。
The const variables are stored in Read Only Area.That is Code Segment area of memeory.
const 变量存储在只读区,即内存的代码段区。

