Overview scope - determines when an identifier can be referenced in a program storage class -...
-
date post
22-Dec-2015 -
Category
Documents
-
view
231 -
download
1
Transcript of Overview scope - determines when an identifier can be referenced in a program storage class -...
Overview
• scope - determines when an identifier can be referenced in a program
• storage class - determines the period of time during which that identifier exists in memory.
• linkage - determines when an identifier is visible outside a file.
scope example
int main () { int xSquared; for (int x = 1; x <= 10; x++) { xSquared = Square (x); cout << xSquared << “ “ ; } cout << result; // would cause a compiler error return 0;}int Square (int y) { int result; result = y * y; return result;}
scope
• file scope
• block scope
file scope
• identifier declared outside any function
• may be referenced by any code which occurs after the identifier is declared
• global variables, function prototypes
file scope#include <iostream>using namespace std;
void Function1 ( );int a1 = 0; char a2 = ‘A’;
int main(){ Function1(); cout << a1 << a2 << endl; return 0;} void Function1 (){ cout << a1 << a2 << endl;}
global variables
• Avoid them!
• pass parameters to a function instead.
block scope
• identifiers declared within a block may only be referenced inside the block.
• block - section of code bounded by braceswhile (true) { statement1; statement2; }a function is a block
block scopeint main () { Function1 (); cout << i; // compilation error }
void Function1 () { int i = 0; // i may not be used within main
while (i <= 4 ) { int j = i + 3; // j may only be used within the while loop cout << j; i++; // i may be used in while loop } cout << j << endl; // compilation error}
block scope - function parametersint main () { int first = 0; Function1 (first); cout << initialValue; // compilation error }
void Function1 (int initialValue) { int i = initialValue; // initialValue may be used in here
while (i <= 4 ) { int j = i + 3; cout << j; i++; }
block scope - same name in inner loop
for (int i = 1; i <= 2; i++) { for (int i = 1; i <= 2; i++) { // this is a different variable i cout << ‘x’; } cout << endl;} Walk through this
Confusing. Should be avoided.
Common programming error
• Accidentally using the same name for an identifier in an inner block
Exercises1) What is the scope of each identifier in the following code:int j = 3; int Cube(int);
int main () { cout << j << " cubed is " << Cube(j) << endl; return 0;}
int Cube (int operand) { int result; void PrintDebug (char); int j = 4;
result = operand * operand * operand; PrintDebug(‘C’); return result; }
void PrintDebug (char debugChar) { cout << debugChar << endl;}
Find the error in the following code. int printChar (int printValue); int main () { int pattern = 3, pattern2 = 4; for (int i = 1; i <=4 ; i++) { // print out a 34 pattern 4 times. printChar(pattern); printChar(pattern2); } return 0;}int printChar( int printValue) { int pattern = 3; cout << pattern; return pattern; }
More Exercises
storage class
• Warning! This topic covers things that are not in chapter 3 in your textbook. Covered in chapter 9.1 and 9.2 of your textbook but the author assumes you know some things about pointers and other things that we haven’t learned yet so his explanations would be confusing.
storage class
• Determines the period of time that an identifier exists in memory.
Static duration - for entire duration of the
program
Automatic duration - just briefly
4 storage classes
• auto (automatic duration)
• register (automatic duration)
• static (static duration)
• extern (static duration)
automatic duration
• exist only while the block they are in is active.
• created when block is entered
• destroyed when block is exited.
automatic duration
int CalculateTotal (int y) {
int result = 0;
result = result + y;
return result;
}
result and y are created when function Square begins execution and destroyed after the return statement is executed.
automatic duration
int CalculateTotal (int y) {
int result = 0;
result = result + y;
return result;
}
auto int result; auto is default so it’s left out.
registerint CalculateTotal (int y) {
register int result = 0;
result = result + y;
return result;
}
register specifier tells compiler that this variable is going to be used a lot and to save it in a CPU register.
Don’t use this. Compiler’s do a better job of deciding what to put in registers than you do.
static duration
• exist when the program begins execution and are not destroyed until the program terminates execution.
static durationint CalculateTotal (int y) {
static int result = 0;
result = result + y;
return result;
}
result is created when the program begins and is not destroyed when CalculateTotal exits.
Initialization only happens once when result is created.
The scope of result has not changed.
static and extern keywords
• perform two jobs
• storage class is static duration
• defines linkage.
g++
• preprocessor
• compilation
• link - links your code with libraries.
links code from one file with code in
another file
Linkage
cout << “Hello”;
main.cpp: library source file:
cout function
code for main function ... cout << “Hello”;
cout function code ... return;
Linker’s symbol table
cout function 1000
Symbol Address
• Contains functions and variables that are visible outside each source code file. • Programmer controls what goes in here by using static or extern keywords
Linkage
• Static - Internal linkage (its name is not visible from outside the file in which it is declared).
• Extern - External linkage (its name is visible from files other than the one in which it's defined).
• Declarations of variables and functions with file scope are external by default
double CalculateAverage(double runningTotal, int numberOfValues); int main () { cout << "Average is " << CalculateAverage(1000, 10); return 0;}
CalculateAverage is declared with file scope and has external linkage by default.
To change to internal linkage use:static double CalculateAverage( double runningTotal, int numberOfValues);
Linkage
Variable attributes
• name
• address
• data type
• scope (file, block)
• storage class (automatic duration, static duration)
• linkage (internal to a file, external)
1) Find the error in the following code. How would you fix it?int Sum (int grade);
int main () { int grade, sum, count = 0; for (int i = 1; i <= 3; i++) { cout << "Enter next grade: "; cin >> grade; sum = Sum (grade); count++; } if (count != 0) cout << "Average is " << sum/count << endl; return 0;}
int Sum (int number) { int sum = 0; sum += number; return sum; }