10 - Static Variables, Scope, Lifetime

10 - Static Variables, Scope, Lifetime - Engineering 101...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Quote of the Day Our greatest glory is not in never falling, but in getting up every time we do. - Confucius Static Variables / Scope / Lifetime Modifications to the Usage of Data Usually when an identifier is declared the computer sets aside memory in which to store the associated data. The memory is then available for use. It can be read and altered during the course of the program. However, sometimes we may want to treat certain data specially. Constants If you want a variable to never change in the context of the code you can define it to be a const const double speedoflight = 3e8; Note that const does not replace the type, but rather it modifies the data type. Aliases (References) We can declare a new variable that is a stand-in for an standother variable. double v = 5.0; double & w = v; cout << w << endl; endl; The above code will print 5.0 because v and w are the same variable. Note: References cannot be changed after they are created (i.e. you can't make `w' refer to something else). Aliases (References) We have already used such variables implicitly when we do pass-by-reference. pass-byWe can make a reference that is a const. That means that the reference version cannot be changed. double v=5.0; const double & w = v; v = v + 1.0; w = w + 1.0; This can be useful for passing data without having to copy it, but also without allowing changes. 1 What is output to the screen? int main() { int A = 10; int & B = A; B = 12; if (A == 10) B = 6; else B = 7; A = 5; cout << A << B << endl; } Scope Each identifier has a context in which it is meaningful. Outside that context it cannot be referred to or used. This context is called its scope. scope. Think of it like people in a room and only being able to talk to the people in the room. 1 57 2 56 3 55 4 None of the above Scope An identifier in a compound statement has local scope. scope. It is in scope from the time it is declared to the end of the enclosing braces. The left curly brace is like entering the room and the right is like leaving the room. void f ( double x) { int i = 1; cout << i << " "; int j = 2; cout << j << endl; cout << x << endl; } Scope x i j A function parameter is in local scope from the beginning of the function body. Scope You cannot declare two identifiers with the same name in the same compound statement. statement. There can only be one Jeff Ringenberg in this room. If there were more than one, who would you listen to? Scope You can declare an identifier with the same name in a different compound statement. statement. There can be another Jeff Ringenberg in another room. You could listen to him, but he wouldn't be as cool... 2 Scope If there are different possible identifiers that could be referred to, the most local version is always chosen. The less local versions are chosen. "hidden". "hidden". If the person you want to talk to isn't in the room, you have to keep walking to more and more outer rooms until you find the person. Once you find the person, you stop looking. Scope main () { int i = 5; { cout << i << endl; int i = 2; cout << i << endl; } } // This will print 5 // This will print 2 What will output to the screen? int main( ) { int i = 3; while (i > 0) { cout << i; int i = 5; cout << i; i--; --; } return 0; } Lifetime The lifetime is the span of time during which a particular data object can be accessed. For identifiers in a compound statement this lasts from the line of the declaration to the time when the closing brace for the compound statement is reached. reached. Think of it like when a person enters a room. Once that person enters, you can talk to, and no longer have to go looking for, him/her. 1 35251505 2 352515 3 353535 4 None of the above Lifetime The lifetime of parameters in functions or procedures is the time spent in the function or procedure body until control returns to the caller. When the lifetime expires the data object is returned to memory for general use. Referring to the identifier after this time can produce strange results. Lifetime main () { int i = 10; while (i > 0) { if (i > 5) { int j = i; // j has no lifetime unless i > 5 cout << j << endl; } cout << i << endl; i--; --; } } 3 Global Constants Some objects have local scope. A good example are functions and procedures themselves. You can make identifiers have global scope. This is a bad idea for non-constant identifiers, since nonthey can change in a procedure or function without explicitly being passed in. int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; } int main () { swap(s,t); cout << s << ` ` << t << endl; return 1; } Scope and Lifetime What does this program do? int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; } First s and t int main () are declared { swap(s,t); cout << s << ` ` << t << endl; return 1; } Scope s 1 t 2 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; } main( ) int main () begins { execution swap(s,t); cout << s << ` ` << t << endl; return 1; } Scope s 1 main t 2 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; } main calls int main () swap( s, t ) { swap(s,t); cout << s << ` ` << t << endl; return 1; } Scope s 1 main swap t 2 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; swap is passed return; s and t by } reference as int main () x and y { swap(s,t); cout << s << ` ` << t << endl; return 1; } Scope main s 1 t 2 Swap x y 4 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; swap prints t. return; But there is no } local "t" so it int main () prints global t { swap(s,t); cout << s << ` ` << t << endl; return 1; } Scope s 1 main Swap x y t 2 t? cout 2 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; Now a new local t } Is created and int main () a value is assigned { swap(s,t); cout << s << ` ` << t << endl; return 1; } Scope s 1 main Swap x t 1 y t 2 cout 2 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; swap prints t. } But this time int main () it prints local t { swap(s,t); cout << s << ` ` << t << endl; return 1; } Scope s 1 main Swap x t 1 y t 2 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; Then x and y } Are swapped int main () { swap(s,t); cout << s << ` ` << t << endl; return 1; } Scope main s 2 t 2 Swap x t 1 y cout 2 1 cout 2 1 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; Then x and y } Are swapped int main () { swap(s,t); cout << s << ` ` << t << endl; return 1; } Scope main s 2 t 1 Swap x t 1 y int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; The procedure } returns int main () { swap(s,t); cout << s << ` ` << t << endl; return 1; } Scope s 2 main Swap x t 1 y t 1 cout 2 1 cout 2 1 5 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; Then, returning return; to the main the } global s and t are int main () printed { swap(s,t); cout << s << ` ` << t << endl; return 1; } cout 2 1 21 Scope s 2 t 1 What will be the 1st cout in the code? 1- 2 10 2- 2 12 3- 5 10 4- 3 13 What will be the 2nd cout in the code? 1- 2 10 2- 2 12 3- 4 14 4- 3 13 What will be the 3rd cout in the code? 1- 2 10 2- 2 14 3- 4 14 4- 3 13 Next Lecture Strings 6 ...
View Full Document

Ask a homework question - tutors are online