Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.

Similar presentations


Presentation on theme: "Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address."— Presentation transcript:

1 Variables Names Bindings Type Scope

2 L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address Right hand side represents a value 100 101 102 103 A B C D 3 12 5 -2 Statement L-Value R-Value C = B 10212 D = A + B10315 A = 11001 location Memory location Name

3 Managing Names Global Data -> everyone sees it i,j,k,l,m,n, o,p,r,s,t,u,v, w,x,y,z,… Others accessing the same data -knowingly different expectations -unknowingly colliding with other programs forget to declare, …no warning

4 Tools for Name Management Declaring in blocks (ifs and loops) Declaring in procedures (Declaring in files/c/c++) Declaring in classes Declaring in namespaces Declaring as global

5 C++ namespaces namespace myvars { int a, b; } namespace yourvars { int a,b; } cout << myvars::a << endl; cout << yourvars::b << endl; {using namespace myvars; cout << b << endl; }

6 C++ iostream #include int main () { std::cout << "ANSI-C++\n"; return 0; } #include using namespace std; int main () { cout << "ANSI-C++\n"; return 0; }

7 Aliases Makes readability and verification difficult Two ways to represent a single value –reference parameters –pointers c unions : used to provide dual interpretation, tricky fortran equivalence INTEGER A(1000), b(400) EQUIVALANCE (A(1), B(1)) A(1) = 23 B(1) has the SAME value! Usually for conserving memory required of large tables. union { int a; int b; } twoval; twoval x; x.a=7; cout << x.b; // OUTPUTS 7

8 Binding Times Language design : types of data Language implementation : representation of integer Compile : initialization of some data values (constants) Load : real address of global data Link : which version of the library is linked in Execute –beginning of program –beginning of procedure –beginning of block –in statement execution

9 Binding Times Understanding of the language depends upon knowing WHEN things happen STATIC : before run-time DYNAMIC : during run-time There are more specific times but these are commonly used

10 Type Binding Static –Explicit Declaration - most languages –Implicit Declaration - Fortran (I-N are int) Dynamic –Usually interpreted languages –APL L <- 12 3 56 L <- “abc” Inference –exclusively in some languages such as ML (p.166) –routinely in expression evaluation

11 Dynamic Typing Requires some type of descriptor Expensive run-time effort, difficult to find errors Integer array x[10] x integer array 10 (values) …..Name …..Type …..array or scalar …..size …..data

12 Memory Allocation Global data : static Stack Dynamic : local data for procedures Heap Dynamic –c++ new and delete operators same for java, pascal, etc –some languages such as APL allocate only when assigned data –efficient memory management –run-time expense

13 Examples in c++ #include int i; // i is static global int double(int x) { int temp; // temp is stack dynamic temp = 2 * x; return temp; } void main() { int *j; // i is explicit heap-dynamic j = new(int); *j=6; *j=*j + 4; i=double (i); cout << i; }

14 C static Most local data in a procedure is stack- based On return, the activation record is “popped” and memory is reused on subsequent calls Can local values be retained between calls? In C, declare the variable static to make it retain one memory location between calls and/or share one location for recursive calls

15 Type Checking One of the more subtle issues of programming expression evaluation and assignment passing parameters runtime check - difficult and expensive but part of interpreters compile-time check - more typical

16 c is WEAKLY typed #include int test(x) int *x; { *x =*x + 7; return 0; } main() { float j; j = 4.3; test(&j); printf("%f\n",j); } Output -> 4.300004 Look for example programs: ~dgame/www/cs310/examples files named typetest.c and typetest2.c

17 c++ is STRONGLY typed #include int test(int & x) { x =x + 7; return 0; } main() { float j; j = 4.3; test(j); cout << j << endl; } Output -> 4.3 doesn’t pass as ref! Look for example program: ~dgame/www/cs310/examples file named typetest2.cpp

18 Name Type Compatability Pascal –type celsius = real; – fahrenheit = real; –These two are NOT the same type That IS the intended effect. You do not want the two temperatures mixed. Application dictates what is compatible C++ uses name compatibility

19 Structure Type Compatability Basically asks are the implementations the same Not easy to determine for the compiler when there are ways to equivalence types Some languages (pascal) don’t specify which so it becomes implementation dependent –example below shows that point Type type1 = array [1..10] of integer; type2 = array [1..10] of integer; type3 = type2; type1 and type2 are not compatible, but type2 and type3 are … not strictly name equivalence

20 Scope Rules How does one determine which data is visible to a procedure? Static rules –c++ class members have a set of rules –c/c++ have other data with a unique set of rules –pascal/algol/pl1 block structured rules –others Dynamic rules –reverse through the calling chain

21 Block Structured Program t(input,output); var x,y:integer; procedure one() var a,x:integer; begin..end; (* one *) procedure two() var b,c:integer; procedure twoa() var d,x : integer; begin..end; (* twoa *) begin..end; (* two *) procedure three() var d,x:integer; begin..end; (* three *) begin..end. (* main routine *) t x t y t one a one,x one y t two b two, c two, x t y t three d three,x three y t twoa d twoa, x twoa, b two, c two, y t

22 C and C++ files Any variables declared outside of a function are global. Those variables are available to all other files In order to access them, you must declare the variable extern The sum of all files compiled can not have any global names the same or the declaration is ambiguous

23 #include … int i; void main() { } #include … int i; void one() { } ILLEGAL #include … int i; void main() { } #include … int j; void one() { } #include … extern int j; void two() { } LEGAL : Third file accesses j in second file

24 Dynamic scope (assume pascal does) Use the same example for block structured pascal: If calls are made as follows: main calls three three calls two A reference by procedure two to x will access the x from three main three two Activation stack Access determined by calling sequence NOT program structure! main twothree

25 Dynamic Scoping Interpreted languages –APL –LISP Extremely difficult to debug Can’t type check parameters All data potentially accessible by other procedures Slow to find parameters Even later dialects of LISP converted

26 Data Initialization When does a variable become initialized when you do something like – int i=0; Generally compilers will store zeros in memory reserved for static variables and copy it into memory when running, so the data is initialized before execution begins Local data in procedures obviously can not do this: must accomplish at run-time

27 Variables Names Bindings Type Scope


Download ppt "Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address."

Similar presentations


Ads by Google