Overview scope - determines when an identifier can be referenced in a program storage class -...

30
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.
  • 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 -...

Page 1: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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.

Page 2: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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;}

Page 3: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

scope

• file scope

• block scope

Page 4: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

file scope

• identifier declared outside any function

• may be referenced by any code which occurs after the identifier is declared

• global variables, function prototypes

Page 5: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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;}

Page 6: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

global variables

• Avoid them!

• pass parameters to a function instead.

Page 7: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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

Page 8: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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}

Page 9: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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++; }  

Page 10: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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.

Page 11: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

Common programming error

• Accidentally using the same name for an identifier in an inner block

Page 12: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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;}

Page 13: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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

Page 14: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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.

Page 15: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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

Page 16: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

4 storage classes

• auto (automatic duration)

• register (automatic duration)

• static (static duration)

• extern (static duration)

Page 17: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

automatic duration

• exist only while the block they are in is active.

• created when block is entered

• destroyed when block is exited.

Page 18: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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.

Page 19: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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.

Page 20: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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.

Page 21: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

static duration

• exist when the program begins execution and are not destroyed until the program terminates execution.

Page 22: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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.

Page 23: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

static and extern keywords

• perform two jobs

• storage class is static duration

• defines linkage.

Page 24: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

g++

• preprocessor

• compilation

• link - links your code with libraries.

links code from one file with code in

another file

Page 25: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

Linkage

cout << “Hello”;

main.cpp: library source file:

cout function

code for main function ... cout << “Hello”;

cout function code ... return;

Page 26: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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

Page 27: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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

Page 28: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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

Page 29: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

Variable attributes

• name

• address

• data type

• scope (file, block)

• storage class (automatic duration, static duration)

• linkage (internal to a file, external)

Page 30: Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.

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; }