Binding COMP360
One Ring to rule them all, One Ring to find them, One Ring to bring them all, And in the darkness bind them. inscription on the One Ring J. R. R. Tolkien
Reading Read chapter 10 of the textbook
Binding A binding is an association between two things, such as a name and the thing it names A name is exactly what you think it is Most names are identifiers symbols (like '+') can also be names The scope of a binding is the part of the program in which the binding is active from Programming Language Pragmatics, 4 th ed, by Michael Scott
What are we binding? At some point a variable name is bound to a memory location In Java, the keyword this means this object. It is bound to a specific object when the method is executed Keywords (such as final, synchronized or long) are bound to a specific concept at language design
Binding Time Binding Time is the point at which a binding is created or, more generally, the point at which any implementation decision is made Some bindings are made before the program is executed Other bindings are made during program execution from Programming Language Pragmatics, 4 th ed, by Michael Scott
Binding Time Examples Language design time meaning of keywords Language implementation time implementation of types Program writing time names to concepts Compile time variable names to relative addresses, program statements to machine language Link time - layout of whole program in memory
Run Time Binding Examples Program load variables and methods assigned to physical addresses Method Entry actual parameter values bound to formal parameters, local variables bound to memory locations
Static and Dynamic The terms STATIC and DYNAMIC are generally used to refer to things bound before run time and at run time, respectively The period of time from creation to destruction is called the LIFETIME of a binding If an object outlives binding, it's garbage If a binding outlives an object, it's a dangling reference from Programming Language Pragmatics, 4 th ed, by Michael Scott
Pros and Cons In general, early binding times are associated with greater efficiency Later binding times are associated with greater flexibility Compiled languages tend to have early binding times Interpreted languages tend to have later binding times We generally talk about the binding of identifiers to the variables they name from Programming Language Pragmatics, 4 th ed, by Michael Scott
When is a value bound to dog? int mymethod( double dog ) A. Writing of the program B. Compile time C. Program linking D. Program load E. Method entry
Storage Management Storage Allocation mechanisms Static Stack Heap Static allocation for code globals static or own variables explicit constants (including strings, sets, etc) scalars may be stored in the instructions from Programming Language Pragmatics, 4 th ed, by Michael Scott
Call stack for parameters local variables temporaries Stack Why a stack? allocate space for recursive routines (not necessary in FORTRAN no recursion) reuse space (in all programming languages) from Programming Language Pragmatics, 4 th ed, by Michael Scott
Stack Contents Contents of a stack frame arguments and returns local variables temporaries bookkeeping (saved registers, line number static link, etc.) Local variables and arguments are assigned fixed OFFSETS from the stack pointer or frame pointer at compile time from Programming Language Pragmatics, 4 th ed, by Michael Scott
Binding an Array Size Binding when program is written in myprog.c int rabbit[123];
Binding an Array Size Binding based on a configuration file in config.h #define SIZE 123 in myprog.c #include <config.h> int rabbit[size];
Binding an Array Size Binding at execution time in myprog.c int arraysize; // size of the array cin >> arraysize; // read size int *rabbit; // pointer to array rabbit = new int[arraysize];
Binding an Array Size Dynamic size of array-like data structure in myprog.java java.util.arraylist<int> rabbit = new java.util.arraylist<>();
Variable Scope The Scope of a variable refers to where you can use a variable Where you declare a variable determines where it can be used The scope of a binding is the part of the program in which the binding is active
Variable Range Variables defined in a block can only be used in that block following the variable declaration { // you cannot use the variable moth here double moth = 72.5; // you may use the variable moth here // this is the scope of moth // The variable moth is not allowed here
Out of Scope int cat; if (whatever == 0) { int dog = 1; else { int dog = 2; cat = dog * 3; // Error, dog is out of scope
This works int cat, dog; if (whatever == 0) { dog = 1; else { dog = 2; cat = dog * 3; // Only one dog variable
Method Parameter Scope The parameters defined in a method header can be used throughout that method Method parameter variables cannot be used in another method void amethod( int dog, double cat ) { You can use dog and cat throughout this method
Declarations First Java and C++ allow you to put variable declarations anywhere in a program It is usually advantageous to declare a variable at the beginning of a block Some older programming languages required you to put all variable declarations at the beginning of a block Fortran All declarations are before executable statements Cobol declarations are in the data division
for Loop Scope A loop counter variable only has scope in the loop for (int cow = 0; cow < 28; cow++) { // loop body // the loop counter, cow, can be used here // You cannot use cow after the loop
Local Variables public static void main( ) { double cat = 5, bird = 47; Scope of cat = myfunc( bird ); cat and bird System.out.print(cat); double myfunc(double cow) { double bull; bull = cow * 2.0; return bull; Scope of cow and bull
Multiple Variable with the Same Name In different parts of a program, you can use the same variable name, but it will mean a different variable This can be confusing and should be avoided
Local Variables (Tricky) public static void main( ) { double cat = 5, bird = 47; Scope of cat = myfunc( bird ); cat and bird System.out.print(cat); int myfunc(double cow) { int bird; bird = (int)cow * 2.0; return bird; Scope of cow and bird (different bird)
Variables A C++ or Java method can use three different types of variables local variables defined in the method parameter variables object instance variables Object instance variables can be used in any of the methods of the class
Name Collision public class Collide { int rat = 3; public int square( int rat ) { return rat * rat; Collide thing = new Collide(); int turtle = thing.square( 2 ); turtle is set to 4
Variable Priority A method cannot have a local variable the same name as a parameter variable If a class instance variable has the same name as a local variable or parameter, the method will always use the local variable or parameter
More Name Collisions public class Collide { int rat = 3; public int square( int mouse) { int rat = mouse * mouse; return rat; The class instance variable rat is a different memory location from the local variable rat
Name Collision public class Collide { int rat = 3; public int square( int rat ) { return this.rat * rat; Collide thing = new Collide(); int turtle = thing.square( 2 ); turtle gets the value 6
Separate Compilation Java, C++ and many languages allow modules to be compiled separately Rules for how variables work with separate compilation are messy In C++, static on a function or variable outside a function or private in Java means it is usable only in the current source file This static is a different notion from the static variables inside a function
What is printed by this program? public class Click { static int crow= 5, raven= 7; static void myfunc(int parrot) { int crow; crow = parrot + 1; System.out.print( crow ); public static void main( ) { Click ques = new Click(); ques.myfunc( 3 ); System.out.print( crow ); A. 4 4 B. 4 5 C. 5 5 D. none of above
What is printed by this program? public class Click { static int crow= 5, raven= 7; static void myfunc(int parrot) { // changed here crow = parrot + 1; System.out.print( crow ); public static void main( ) { Click ques = new Click(); ques.myfunc( 3 ); System.out.print(crow ); A. 4 4 B. 4 5 C. 5 5 D. none of above
Created Elsewhere extern on a C++ variable or function means that it is declared in another source file In Java and C++, method headers without bodies are extern by default
No class next week Spring break is March 5 to March 11, 2017