/*----------------------------------------------------- Script for AdSense -----------------------------------------------------*/ /* */ /* Footer ----------------------------------------------- */ #footer { clear: both; text-align: center; color: #333333; } #footer .widget { margin:.5em; padding-top: 20px; font-size: 85%; line-height: 1.5em; text-align: left; } /** Page structure tweaks for layout editor wireframe */ body#layout #header { width: 750px; } -->

Monday, April 19, 2010

Storage Class Specifier

What is storage class specifer?

There are four storage class specifiers supported by C. They are
extern
static
register
auto

These tell the compiler how the variable that follows should be stored.

extern variable:

Because C allows separately compiled modules of a large program to be linked together to speed up compilation and aid in the management of large projects, there must be some way of telling all the files about the global variables required by the program. The solution is to declare all of your globals in one file and use extern declarations in the other


extern statements are declarations, but not definitions. They simply tell the compiler that a definition exists elsewhere in the program.



File 1:

int
x,y;

void
fun1()
{

x = 22;
}
File 2:

extern
int x,y;

void
fun2()
{

y = x/2;
}

Although extern variable declarations can occur inside the same file as the global declaration, they are not necessary. If the C compiler encounters a variable that has not been declared, the compiler checks whether it matches any of the global variables. If it does, the compiler assumes that the global variable is the one being referenced.

static variable:
static local variable.
When static is applied to a local variable it causes the compiler to create permanent storage for it in much the same way that it does for a global variable.
When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.


// static1.cpp
#include
using namespace std;
void
showstat( int curr )
{

static
int nStatic; // Value of nStatic is retained // between each function call
nStatic += curr;
cout "nStatic is " << nStatic << endl;
}

int
main()
{

for
( int i = 0; i < 5; i++ )
showstat( i );
}

output:
nStatic is 0
nStatic is 1
nStatic is 3
nStatic is 6
nStatic is 10


static global variable:
When the specifier static is applied to a global variable, it instructs the compiler to create a global variable that is known only to the file in which the static global variable is declared.
This means that even though the variable is global, other routines in other files may have no knowledge of it or alter its contents directly;

Register Variable:
The register specifier requests the compiler to store a variable declared with this modifier in a manner that allows the fastest access time possible. For integers and characters, this typically means in the register of the CPU rather than in memory, where normal variables are stored.

In Borland C++, the register specifier may be applied to local variables and to the formal parameters in a function.
You cannot apply register to global variables.
Also, because a register variable may be stored in a register of the CPU, you cannot obtain the address of a register variable. (This restriction applies only to C, and not C++.)

In general, operations on register variables occur much faster than on variables stored in main memory. In fact, when the value of a variable is actually held in the CPU, no memory access is required to determine or modify its value. This makes register variables ideal for loop control.

It is important to understand that the register specifier is just a request to the compiler, which the compiler is free to ignore. However, in general, you can count on at least two register variables of type char or int actually being held in a CPU register for any one function. Additional register variables will be optimized to the best ability of the compiler.

No comments: