Review 2 1 Binding q What is binding
Review 2 1
Binding q What is binding? q When does binding of names occur? q What is a variable? 2
Binding and Attributes q Name and describe 6 attributes that can be bound to a variable name? q When does binding of names occur? q What is an alias? 3
Three Memory areas q The memory area allocated for a program's data is divided into 3 areas. What are their names? n n n q What kind of values are placed in each area? n n n q Name a language that allocates all storage in only one of these area? 4
Scope q There are 2 types of scope rules. What are they called? n n Most languages use which type of scope? q C: q int x = 10; int fun(int x) { x = x * x; return x; } int main( ) { int y = x; { int x = fun(y); } // what is x ? // what is y ? 5
Linking of names q q What is the name of the software that joins object files and resolves external references in object files? Does this program verify data types? /* File 1 */ char *foo; extern double bar; . . . printf("%s", foo); printf("%f", bar); /* File 2 */ extern double foo; char* bar = "foo!"; . . . printf("%d", foo); printf("%s", bar); 6
Referencing environment q q What is a referencing environment? In the calculator, how do we store the referencing environment? Is the Symbol Table needed by the binary (executable) program? q Is the symbol table included in the output of the linker (compiled program)? q On Linux, how can you get rid of the symbol table? q 7
Symbol Table q q What information is in the symbol table? Is the Symbol Table needed by the binary (executable) program? Is the symbol table included in the output of the linker (compiled program)? On Linux, how can you get rid of the symbol table in a binary (compiled) program file? 8
Remembering values q I want to write a C function that tells me how many times the function has been called. counter( ); . . . q // returns 1 // returns 2 // returns 3 how can I write a counter() function that remembers how many times it has been called? (without using global variables) int counter( ) { static int count = 0; return ++count; } 9
Call By. . . q What are the names of the 3 methods of passing parameters to subroutines? int fun(int x) { x = x * x; return x; q Describe each one. } int main( ) { int y = 20; printf( fun(y) ); q What language uses printf( y ); which param passing method? } Java, C, C#, Fortran-77 // what is printed? 10
Test the param passing method? q Design a simple program to determine which parameter passing method is being used. void fun( ) { } int main( ) { } 11
Values versus References What is a value data type? q What is a reference data type? q int fun(int x) { x = x * x; qreturn x; } int main( ) { int x = 10; int y = fun(x); // what is x ? // is (y == x) ? Integer fun(Integer x) { x = x * x; return x; } int main( ) { Integer x = 10; Integer y = fun(x); // what is x ? // is (y == x) ? 12
What does this do? % what does this do? sub([], List). sub([H|T], List) : member(H, List), sub(T, List). 13
What does this do? Underscore (_) means "don't care". % what does this do? It accepts any value. foo([], _, []). foo([H|T], List, [H|P]) : member(H, List), foo(T, List, P). foo([H|T], List, P) : not( member(H, List) ), foo(T, List, P). 14
Memory and VMT Person - name Person(name: String) get. Name() : String Person kung = new Foo("Kung", 20); • Draw a picture of the contents and layout of memory used by kung. • Draw the VMT for Foo, showing methods in the correct order. Foo - next. ID : int - age : int Foo( String, int) get. Age() : int to. String() : String Refactor: • how does the picture change if next. ID is moved to the Person class? 15
- Slides: 15