Storage Class in C/C++

The storage classes are same for C & C++ except the storage class mutable is only for C++.


Auto

It is the default storage class for all local variables if it is not explicitly specified as static. 
Usually local variables can be stored either on the stack or in a data segment depending on whether they are auto or static.

Memory Allocation - Run time

Variable/Array - Size is known at compile time, but memory is allocated during the execution.
                   
Storage Segment - Stack (part of executable)

Initial Value - Garbage

Scope - Within Block 

Life - End of Block

Static

It instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying each time it comes into and goes out of scope. Therefore, it maintains the value between function calls. 

If a function/variable is static, it is local to the file/function where it is declared.

Memory allocation -  Compile time/Link time,
                                   because it has to give the value to the assembler

Storage Segment - Data segment (part of executable)


Initial Value - Zero

Scope - Within Block 

Life - Throughout the program

Extern

Extern is used to give a reference to global variable that is visible to all files. 

extern is overloaded for several uses. For global variables, it means that it is declaring the variable, not defining it. This is useful for putting global variables in headers. If you put this in a header:
int someInteger;
Each .cpp file that includes that header would try to have its own someInteger. That will cause a linker error. By declaring it with extern, all you're saying to compiler is that there will be a someInteger defined somewhere in the code and it will connected with the linker. It basically avoids name collision. 
extern int someInteger;
Now, in a .cpp file, you can define int someInteger, so that there will be exactly one copy of it.
Storage Segment - Data segment (part of executable)
Initial Value - Zero
Scope - Multiple files
Life - Throughout the program
Click here to learn more about static & extern with examples.

Register 

Direct the compiler to use register to store the variable.

Register is used when you wish to force a value to be kept in register rather than kept in RAM. For instance you might do the following:
register int x;
This would let the compiler know that you want this int to be placed in the CPU register, which should give you faster access to it. However, your compiler is allowed to ignore this keyword in the process of optimization and most of the time good compilers will place variables in registers if they need to be anyway. Hence, it is not always stored in register though it has been tagged with register keyword. 
Register is concerned in the compiler optimisation before a decade, but register keyword has no value if the optimisation is turned on. In this case, compiler ignores it. It will be stored on the stack. 
Since there is no address, we can't have & unary operator associated to it. This means pointer will not work as register. 
Storage Segment - CPU Register
Initial Value - Garbage
Scope - Within Block 

Life - End of Block

Mutable

The mutable specifier applies only to class objects, which are discussed later in this tutorial. It allows a member of an object to override const member function. That is, a mutable member can be modified by a const member function.

Volatile (Must Read *)

In computer programming, particularly in the C, C++, and C# programming languages, a variable or object declared with the volatile keyword usually has special properties related to optimization and/or threading. Generally speaking, the volatile keyword is intended to prevent the (pseudo)compiler from applying any optimizations on the code that assume values of variables cannot change "on their own."

The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread. The volatile keyword guarantees that changes to a variable in one thread, remains coherent across all threads.

It tells the compiler that it shouldnt optimize the access and access the value from memory and dont cache it. And the value can be changed at any time due to i/o operations.
It's used a lot in low-level embedded systems for talking to hardware, (as well as in multi-threaded code - but there's usually a better way of communicating than through volatiles.
This is widely used in hardware.

Takeaways 

If a variable is stored in Cache, it means an optimisation is applied to that variable. 

Why the array must have a size while declaring ?

The auto storage class array will be stored in Stack which is part of executable file. When a segment is part of executable file, it has a fixed size. Please refer Memory Layout of Process

Click here to learn about Memory layout of C/C++

Reference 



Comments