1 / 30

Understanding Scope and Storage Classes - Helpful Assistant

Learn about scope and storage classes in programming, including file scope, block scope, and global variables. Understand the differences between automatic and static duration storage classes. Avoid common programming errors related to scope.

rpowell
Download Presentation

Understanding Scope and Storage Classes - Helpful Assistant

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

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

  3. scope • file scope • block scope

  4. file scope • identifier declared outside any function • may be referenced by any code which occurs after the identifier is declared • global variables, function prototypes

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

  6. global variables • Avoid them! • pass parameters to a function instead.

  7. block scope • identifiers declared within a block may only be referenced inside the block. • block - section of code bounded by braces while (true) { statement1; statement2; } a function is a block

  8. block scope int 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 }

  9. block scope - function parameters int 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++; }  

  10. 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.

  11. Common programming error • Accidentally using the same name for an identifier in an inner block

  12. Exercises 1) 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; }

  13. More Exercises 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; }

  14. 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.

  15. 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

  16. 4 storage classes • auto (automatic duration) • register (automatic duration) • static (static duration) • extern (static duration)

  17. automatic duration • exist only while the block they are in is active. • created when block is entered • destroyed when block is exited.

  18. 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.

  19. 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.

  20. register int 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.

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

  22. static duration int 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.

  23. static and extern keywords • perform two jobs • storage class is static duration • defines linkage.

  24. g++ • preprocessor • compilation • link - links your code with libraries. links code from one file with code in another file

  25. Linkage main.cpp: library source file: cout << “Hello”; cout function code for main function ... cout << “Hello”; cout function code ... return;

  26. Linker’s symbol table 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

  27. 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

  28. Linkage 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);

  29. Variable attributes • name • address • data type • scope (file, block) • storage class (automatic duration, static duration) • linkage (internal to a file, external)

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

More Related