Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 141COSC 181 – Foundations of Computer ProgrammingClass 2426.9 Storage ClassesEach identifier has several attributesName, type, size and valueAlso storage class, scope and linkageC++ provides five storage-class specifiers: •auto, reg ister , ext ern, mu table and staticIdentifier’s storage classDetermines the period during which that identifier exists in memoryIdentifier’s scopeDetermines where the identifier can be referenced in a program36.9 Storage Classes (Cont.)Automatic storage classDeclared with keywords auto and registerAutomatic variablesCreated when program execution enters block in which they are definedExist while the block is activeDestroyed when the program exits the blockOnly local variables and parameters can be of automatic storage class Such variables normally are of automatic storage classVariables are auto by default4Software Engineering Observation 6.8 Automatic storage is an example of the principle of least privilege, which is fundamental to good software engineering. In the context of an application, the principle states that code should be granted only the amount of privilege and access that it needs to accomplish its designated task, but no more. Why should we have variables stored in memory and accessible when they are not needed?56.9 Storage Classes (Cont.)Static storage classDeclared with keywords extern and staticStatic-storage-class variables Exist from the point at which the program begins execution Initialized once when their declarations are encounteredLast for the duration of the programStatic-storage-class functionsThe name of the function exists when the program begins execution, just as for all other functionsHowever, even though the variables and the function names exist from the start of program execution, this does not mean that these identifiers can be used throughout the program.66.9 Storage Classes (Cont.)Two types of identifiers with static storage classExternal identifiersSuch as global variables and global function names Local variables declared with the storage class specifier stati cGlobal variables Created by placing variable declarations outside any class or function definitionRetain their values throughout the execution of the programCan be referenced by any function that follows their declarations or definitions in the source file7Software Engineering Observation 6.9 Declaring a variable as global rather than local allows unintended side effects to occur when a function that does not need access to the variable accidentally or maliciously modifies it. This is another example of the principle of least privilege. In general, except for truly global resources such as cin and cout, the use of global variables should be avoided except in certain situations with unique performance requirements.8Software Engineering Observation 6.10 Variables used only in a particular function should be declared as local variables in that function rather than as global variables.96.9 Storage Classes (Cont.)Local variables declared with keyword staticKnown only in the function in which they are declaredRetain their values when the function returns to its caller Next time the function is called, the static local variables contain the values they had when the function last completed If numeric variables of the static storage class are not explicitly initialized by the programmerThey are initialized to zero106.10 Scope RulesScopePortion of the program where an identifier can be usedFour scopes for an identifierFunction scopeFile scopeBlock scopeFunction-prototype scope116.10 Scope Rules (Cont.)File scopeFor an identifier declared outside any function or class Such an identifier is “known” in all functions from the point at which it is declared until the end of the fileGlobal variables, function definitions and function prototypes placed outside a function all have file scopeFunction scopeLabels (identifiers followed by a colon such as start:) are the only identifiers with function scopeCan be used anywhere in the function in which they appearCannot be referenced outside the function bodyLabels are implementation details that functions hide from one another126.10 Scope Rules (Cont.)Block scopeIdentifiers declared inside a block have block scopeBlock scope begins at the identifier’s declarationBlock scope ends at the terminating right brace (}) of the block in which the identifier is declared Local variables and function parameters have block scopeThe function body is their block Any block can contain variable declarationsIdentifiers in an outer block can be “hidden” when a nested block has a local identifier with the same nameLocal variables declared static still have block scope, even though they exist from the time the program begins executionStorage duration does not affect the scope of an identifier136.10 Scope Rules (Cont.)Function-prototype scopeOnly identifiers used in the parameter list of a function prototype have function-prototype scopeParameter names appearing in a function prototype are ignored by the compilerIdentifiers used in a function prototype can be reused elsewhere in the program without ambiguityHowever, in a single prototype, a particular identifier can be used only once14Common Programming Error 6.12 Accidentally using the same name for an identifier in an inner block that is used for an identifier in an outer block, when in fact the programmer wants the identifier in the outer block to be active for the duration of the inner block, is normally a logic
View Full Document