DOC PREVIEW
UVa-Wise COSC 181 - Foundations of Computer Programming

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 141COSC 181 – Foundations of Computer ProgrammingClass 2426.9 Storage ClassesEach identifier has several attributesName, type, size and valueAlso storage class, scope and linkageC++ provides five storage-class specifiers: •auto, reg ister , ext ern, mu table and staticIdentifier’s storage classDetermines the period during which that identifier exists in memoryIdentifier’s scopeDetermines where the identifier can be referenced in a program36.9 Storage Classes (Cont.)Automatic storage classDeclared with keywords auto and registerAutomatic variablesCreated when program execution enters block in which they are definedExist while the block is activeDestroyed when the program exits the blockOnly local variables and parameters can be of automatic storage class Such variables normally are of automatic storage classVariables 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 classDeclared with keywords extern and staticStatic-storage-class variables Exist from the point at which the program begins execution Initialized once when their declarations are encounteredLast for the duration of the programStatic-storage-class functionsThe name of the function exists when the program begins execution, just as for all other functionsHowever, 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 classExternal identifiersSuch as global variables and global function names Local variables declared with the storage class specifier stati cGlobal variables Created by placing variable declarations outside any class or function definitionRetain their values throughout the execution of the programCan 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 staticKnown only in the function in which they are declaredRetain 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 programmerThey are initialized to zero106.10 Scope RulesScopePortion of the program where an identifier can be usedFour scopes for an identifierFunction scopeFile scopeBlock scopeFunction-prototype scope116.10 Scope Rules (Cont.)File scopeFor 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 fileGlobal variables, function definitions and function prototypes placed outside a function all have file scopeFunction scopeLabels (identifiers followed by a colon such as start:) are the only identifiers with function scopeCan be used anywhere in the function in which they appearCannot be referenced outside the function bodyLabels are implementation details that functions hide from one another126.10 Scope Rules (Cont.)Block scopeIdentifiers declared inside a block have block scopeBlock scope begins at the identifier’s declarationBlock scope ends at the terminating right brace (}) of the block in which the identifier is declared Local variables and function parameters have block scopeThe function body is their block Any block can contain variable declarationsIdentifiers in an outer block can be “hidden” when a nested block has a local identifier with the same nameLocal variables declared static still have block scope, even though they exist from the time the program begins executionStorage duration does not affect the scope of an identifier136.10 Scope Rules (Cont.)Function-prototype scopeOnly identifiers used in the parameter list of a function prototype have function-prototype scopeParameter names appearing in a function prototype are ignored by the compilerIdentifiers used in a function prototype can be reused elsewhere in the program without ambiguityHowever, 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

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?