Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 963
Matrix Avatar asked Sep 10 '10 12:09

Matrix


People also ask

Where does global static and local register stored?

Local is stored in the stack. Register variables are stores in the registers. Global,static variables are stores in the Heap.

Where are local and global variables stored?

Global variables are stored in the data segment of memory. Local variables are stored in a stack in memory.

Where are register variables stored?

Register variables are stored in registers. Static variable is stored in the memory of the data segment. In register variables, CPU itself stores the data and access quickly.

Where the local variables are stored?

Local variables are stored in memory area of the corresponding function. Scope of a variable is a program part, in which a variable can be referred to. Variables declared inside a block (at the internal level), have the block as their scope.


2 Answers

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

like image 120
Ferruccio Avatar answered Sep 22 '22 04:09

Ferruccio


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.

like image 43
hotpaw2 Avatar answered Sep 20 '22 04:09

hotpaw2