32

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matrix
  • 7,477
  • 14
  • 66
  • 97

5 Answers5

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

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
12

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 Wi-Fi 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 non-volatile memory storage (depending on the existence, implementation, and current state of any demand-paged virtual memory subsystem), to perhaps everything in registers if your application’s 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hotpaw2
  • 70,107
  • 14
  • 90
  • 153
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.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
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.

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

mipadi
  • 398,885
  • 90
  • 523
  • 479
2

Local

Local variables whose scope is within the function. It may be two types, auto or static.

If it is declared simply int var, the compiler treats it as the auto storage class. The auto variables are stored on the stack.

The static variables are stored in the data segment.

The register variables are stored in the CPU. If no registers are available to store variables, then the compiler treats them as auto variable.

The global variables are stored in the data segment area.

The const variables are stored in the read-only area. That is code segment area of memory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kumar123
  • 791
  • 7
  • 21