简体   繁体   中英

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

存储了本地,全局,静态,自动,寄存器,extern,const,volatile变量的位置?

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

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

  • 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 all this applies to C & C++ as well as Objective-C.

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

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.

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.

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.

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.

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

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.

The register variables are stored in CPU.If no registers are available to store variables.then compiler treat as auto variable.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM