Top Banner
Storage Class Specifiers -Reddhi Sekhar Basu
24

Storage Class Specifiers

Oct 31, 2014

Download

Education

Reddhi Basu

Storage Class Specifiers in Object Oriented Programming
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 1. -Reddhi Sekhar Basu

2. The choice of storage class affects a variables storage duration, scope, and linkage. The storage duration of a variable is the portion of program execution during which storage for the variable exists. The scope of a variable is the portion of the program text in which the variable can be referenced. The linkage of a variable determines whether it may be shared by more than one file in the same program. 3. Scope Block Scope Function Scope Program Scope 4. In this section, a block refers to any sets of statements enclosed in braces ({ and }). A variable declared within a block has block scope. Thus, the variable is active and accessible from its declaration point to the end of the block. Sometimes, block scope is also called local scope. For example, the variable i declared within the block of the following main function has block scope: int main() { int i; /* block scope */ . . . return 0; } Usually, a variable with block scope is called a local variable. 5. Function scope indicates that a variable is active and visible from the beginning to the end of a function. In C, only the goto label has function scope. For example, the goto label, start, shown in the following code portion has function scope: int main() { int i; /* block scope */ . . start: /* A goto label has function scope */ . . goto start; /* the goto statement */ . . return 0; } Here the label start is visible from the beginning to the end of the main() function. Therefore, there should not be more than one label having the same name within the main() function. 6. A variable is said to have program scope when it is declared outside a function. For instance, look at the following code: int x = 0; /* program scope */ float y = 0.0; /* program scope */ int main() { int i; /* block scope */ . . return 0; } Here the int variable x and the float variable y have program scope. Variables with program scope are also called global variables, which are visible among different files. These files are the entire source files that make up an executable program. Note that a global variable is declared with an initializer outside a function. 7. Automatic :Storage for the variable is allocated when the surrounding block is executed; storage is deallocated when the block terminates, causing the variable to lose its value. auto register Static : The variable has a permanent storage location, hence it retains its value throughout the execution of the program. static extern 8. External The same variable may be shared by several files Internal The variable is restricted to a single file 9. The auto storage class specifier lets you explicitly declare a variable with automatic storage. The auto storage class is the default for variables declared inside a block. A variable x that has automatic storage is deleted when the block in which x was declared exits. Until C++11, auto had the semantic of a storage duration specifier. Syntax auto variable initializer auto function -> return type 10. You can only apply the auto storage class specifier to names of variables declared in a block or to names of function parameters. However, these names by default have automatic storage. Therefore the storage class specifier auto is usually redundant in a data declaration. Specifies that the type of the variable that is being declared will be automatically deduced from its initializer. For functions, specifies that the return type is a trailing return type. 11. You can initialize any auto variable except parameters. If you do not explicitly initialize an automatic object, its value is indeterminate. If you provide an initial value, the expression representing the initial value can be any valid C or C++ expression. The object is then set to that initial value each time the program block that contains the object's definition is entered. If automatic variables are not initialized they will contain garbage. Note that if you use the goto statement to jump into the middle of a block, automatic variables within that block are not initialized. 12. Objects with the auto storage class specifier have automatic storage duration. Each time a block is entered, storage for auto objects defined in that block is made available. When the block is exited, the objects are no longer available for use. An object declared with no linkage specification and without the static storage class specifier has automatic storage duration. If an auto object is defined within a function that is recursively invoked, memory is allocated for the object at each invocation of the block. 13. The word register is borrowed from the world of computer hardware. Each computer has a certain number of registers to hold data and perform arithmetic or logical calculations. Because registers are located within the CPU (central processing unit) chip, it's much quicker to access a register than a memory location. Therefore, storing variables in registers may help to speed up your program. You can apply this specifier to variables when you think it's necessary to put the variables into the computer registers. 14. However, the register specifier only gives the compiler a suggestion. In other words, a variable specified by the register keyword is not guaranteed to be stored in a register. The compiler can ignore the suggestion if there is no register available, or if some other restrictions have to apply. Because of the limited size and number of registers available on most systems, few variables can actually be put in registers. If the compiler does not allocate a machine register for a register object, the object is treated as having the storage class specifier auto. 15. An object having the register storage class specifier must be defined within a block or declared as a parameter to a function. You can initialize any register object except parameters. If you do not initialize an automatic object, its value is indeterminate. If you provide an initial value, the expression representing the initial value can be any valid C or C++ expression. The object is then set to that initial value each time the program block that contains the object's definition is entered. 16. Objects with the register storage class specifier have automatic storage duration. Each time a block is entered, storage for register objects defined in that block is made available. When the block is exited, the objects are no longer available for use. If a register object is defined within a function that is recursively invoked, memory is allocated for the variable at each invocation of the block. 17. Loop Control Variables: int main() { /* block scope with the register specifier */ register int i; . . . for (i=0; i