C++ Storage classes and qualifiers like auto, extern, register, volatile, const, mutable

(Last Updated On: May 17, 2010)
Learn the Secret

Get  our 2 Free Books

Get these now which land directly to their inbox.
Invalid email address

C++ Storage classes and qualifiers like auto, extern, register, volatile, const, mutable

C++ storage classes include:
Auto: This is the default where variables are automatically created when they are defined and are destroyed at the end of a block containing their definition. They are also not visible outside of their defined block.
Register: This is similar to auto variable but suggests to the compiler to use a CPU register for performance.
Extern: A static variable that is whose definition and placement is determined when object and library modules are combined (also known as linking) to form the executable code file. It can also be visible outside of where it is defined as well. Both C and C++ require that symbols are only defined in a single translation unit (file.c|file.cpp). The extern keyword tells the compiler that a symbol is only a declaration – it is defined somewhere else. If you only ever include a header in a single .c-file, you can get away without the extern. However, if you include it in more than one, the linker will see the same symbol defined in more than one translation unit (one for each .c[pp] file including the header) and rightly flag it as an error.
Storage qualifiers:
Const keyword indicates that memory once initialized cannot be altered by the program.
Volatile keyword indicates that the value at a particular memory location can be altered event though in the program modifies its content. Volatile is to improve the compiler optimization. The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something (external resources) such as the operating system, the hardware, or a concurrently executing thread. For example,
Mutable keyword indicate particular data member of a structure or class can be altered even if a particular variable is defined in a class member function as being const.
structure variable, class, or class member function is constant.
struct data
{
char name[80];
mutable double salary;
}
const data MyStruct = { “Satish Shetty”, 1000 }; //initlized by complier
strcpy ( MyStruct.name, “Shilpa Shetty”); // compiler error
MyStruct.salaray = 2000 ; // complier is happy allowed

?
int volatile nVint;

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!
This entry was posted in Quant Development and tagged , , , , , , , , , on by .

About caustic

Hi i there My name is Bryan Downing. I am part of a company called QuantLabs.Net This is specifically a company with a high profile blog about technology, trading, financial, investment, quant, etc. It posts things on how to do job interviews with large companies like Morgan Stanley, Bloomberg, Citibank, and IBM. It also posts different unique tips and tricks on Java, C++, or C programming. It posts about different techniques in learning about Matlab and building models or strategies. There is a lot here if you are into venturing into the financial world like quant or technical analysis. It also discusses the future generation of trading and programming Specialties: C++, Java, C#, Matlab, quant, models, strategies, technical analysis, linux, windows P.S. I have been known to be the worst typist. Do not be offended by it as I like to bang stuff out and put priorty of what I do over typing. Maybe one day I can get a full time copy editor to help out. Do note I prefer videos as they are much easier to produce so check out my many video at youtube.com/quantlabs