本地、全局、静态、自动、寄存器、outside、const、volatile变量存储在哪里?

时间:2021-10-30 00:06:18

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

本地、全局、静态、自动、寄存器、outside、const、volatile变量存储在哪里?

5 个解决方案

#1


51  

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

    全局变量存储在数据段中(除非编译器可以对它们进行优化,请参阅const),并且从声明点到编译单元的末尾都具有可见性。

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

    静态变量存储在数据段中(除非编译器可以对它们进行优化),并且从声明点到封闭范围的末尾都具有可见性。非静态的全局变量也可以在其他编译单元中看到(参见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 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.

    外部变量存储在数据段中。外部修饰符告诉编译器,一个不同的编译单元实际上是在声明变量,所以不要再创建它的另一个实例,否则在链接时将会出现名称冲突。

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

    const变量可以存储在堆栈上,也可以存储在readonly数据段上,这取决于它们是自动的还是静态的。但是,如果编译器可以确定不能从不同的编译单元引用它们,或者您的代码没有使用const变量的地址,那么可以*地对它进行优化(每个引用可以被常量值替换)。在这种情况下,它不会存储在任何地方。

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

    volatile修饰符告诉编译器,变量的值可能随时因外部影响(通常是硬件)而改变,因此当引用该变量时,它不应该试图优化从内存中重新加载到寄存器中的任何内容。这意味着静态存储。

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

顺便说一句,所有这些都适用于C和c++以及Objective-C。

#2


11  

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存储(取决于是否存在,实现,和任何demand-paged虚拟内存子系统)的当前状态,或许一切在寄存器中如果你的应用程序的大小和一生足够小。

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.

在编译器优化级别,这些变量中的许多可能已经被优化得不存在了。它们不存储在任何地方,除了作为抽象。

#3


5  

Local and auto variables are stored on the stack. Global and static variables are stored in a DATA page. register variables are stored in a register on the CPU if possible, otherwise in the stack. extern, const, and volatile do not specify where the variable is stored; the variable is stored where the other storage specifiers say they are.

本地和自动变量存储在堆栈中。全局变量和静态变量存储在数据页面中。如果可能的话,寄存器变量存储在CPU上的寄存器中,否则存储在堆栈中。extern、const和volatile均未指定变量存储的位置;变量存储在其他存储说明符所说的位置。

#4


2  

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.

本地变量通常存储在堆栈中,在程序的“text”段(在字符串常量的情况下)或在堆中动态分配的全局变量。自动变量通常在函数/方法中使用,并且通常在堆栈中传递(有时也在寄存器中传递,这取决于体系结构)。寄存器变量曾经被存储在寄存器中,但是现在大多数编译器都忽略了寄存器关键字,并将它们放在它们认为合适的地方——堆栈或寄存器中。外部、const和volatile成员都是修饰词,因此它们没有确定的存储位置。

So the short answer is, as usual, "it depends".

所以简单的回答是,像往常一样,“看情况而定”。

#5


1  

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.

作用域在函数中的局部变量。它可以是自动或静态两种类型。如果声明为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变量存储在只读区域中。这是memeory的代码段。

#1


51  

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

    全局变量存储在数据段中(除非编译器可以对它们进行优化,请参阅const),并且从声明点到编译单元的末尾都具有可见性。

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

    静态变量存储在数据段中(除非编译器可以对它们进行优化),并且从声明点到封闭范围的末尾都具有可见性。非静态的全局变量也可以在其他编译单元中看到(参见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 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.

    外部变量存储在数据段中。外部修饰符告诉编译器,一个不同的编译单元实际上是在声明变量,所以不要再创建它的另一个实例,否则在链接时将会出现名称冲突。

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

    const变量可以存储在堆栈上,也可以存储在readonly数据段上,这取决于它们是自动的还是静态的。但是,如果编译器可以确定不能从不同的编译单元引用它们,或者您的代码没有使用const变量的地址,那么可以*地对它进行优化(每个引用可以被常量值替换)。在这种情况下,它不会存储在任何地方。

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

    volatile修饰符告诉编译器,变量的值可能随时因外部影响(通常是硬件)而改变,因此当引用该变量时,它不应该试图优化从内存中重新加载到寄存器中的任何内容。这意味着静态存储。

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

顺便说一句,所有这些都适用于C和c++以及Objective-C。

#2


11  

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存储(取决于是否存在,实现,和任何demand-paged虚拟内存子系统)的当前状态,或许一切在寄存器中如果你的应用程序的大小和一生足够小。

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.

在编译器优化级别,这些变量中的许多可能已经被优化得不存在了。它们不存储在任何地方,除了作为抽象。

#3


5  

Local and auto variables are stored on the stack. Global and static variables are stored in a DATA page. register variables are stored in a register on the CPU if possible, otherwise in the stack. extern, const, and volatile do not specify where the variable is stored; the variable is stored where the other storage specifiers say they are.

本地和自动变量存储在堆栈中。全局变量和静态变量存储在数据页面中。如果可能的话,寄存器变量存储在CPU上的寄存器中,否则存储在堆栈中。extern、const和volatile均未指定变量存储的位置;变量存储在其他存储说明符所说的位置。

#4


2  

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.

本地变量通常存储在堆栈中,在程序的“text”段(在字符串常量的情况下)或在堆中动态分配的全局变量。自动变量通常在函数/方法中使用,并且通常在堆栈中传递(有时也在寄存器中传递,这取决于体系结构)。寄存器变量曾经被存储在寄存器中,但是现在大多数编译器都忽略了寄存器关键字,并将它们放在它们认为合适的地方——堆栈或寄存器中。外部、const和volatile成员都是修饰词,因此它们没有确定的存储位置。

So the short answer is, as usual, "it depends".

所以简单的回答是,像往常一样,“看情况而定”。

#5


1  

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.

作用域在函数中的局部变量。它可以是自动或静态两种类型。如果声明为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变量存储在只读区域中。这是memeory的代码段。