0% found this document useful (0 votes)
3 views27 pages

Lecture 1

The lecture discusses the concepts of names and bindings in programming, emphasizing the importance of names as symbolic identifiers that allow for abstraction and reduce complexity. It covers binding times, the differences between early and late binding, and the lifetimes of objects in memory, including static, stack, and heap allocations. Additionally, it addresses specific programming examples and Java language specifications related to null references and static methods.

Uploaded by

moh570kamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views27 pages

Lecture 1

The lecture discusses the concepts of names and bindings in programming, emphasizing the importance of names as symbolic identifiers that allow for abstraction and reduce complexity. It covers binding times, the differences between early and late binding, and the lifetimes of objects in memory, including static, stack, and heap allocations. Additionally, it addresses specific programming examples and Java language specifications related to null references and static methods.

Uploaded by

moh570kamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

CS 474: OOLE

Lecture 1: Names and Bindings

Instructor: Dr. Mark Grechanik


University of Illinois at Chicago
Names

A name is a mnemonic character string that is used to represent some


object
• Names are alpha-numeric tokens (in many languages)
also called identifiers
• Names allow us to refer to variables, constants,
operations and other programming entities using
symbolic identifiers rather than low-level concepts
Names allow programmers realize abstractions by associating names
with code fragments
• By hiding irrelevant details, abstraction reduces conceptual
complexity
• Programmers can focus on a manageable subset of the
program text
Department of Computer Science, the
University of Illinois at Chicago 2
Binding

Name

Binding

Thing

Department of Computer Science, the


University of Illinois at Chicago 3
Binding Time

A binding is an association between


a name and the thing it names

Binding time is the time at which a


binding is created, i.e.,
implementation decisions are made

Department of Computer Science, the


University of Illinois at Chicago 4
Different Binding Times

Department of Computer Science, the


University of Illinois at Chicago 5
Early and Late Binding Times

Early binding times = greater efficiency


• Compiler decides on the layout of declared
variables in memory and generates efficient code
to access these variables in programs
• Addresses of variables cannot always be
predicted, but their offsets can be defined
Late binding times = greater flexibility
• Interpreters analyze declarations every time the
program executes or subroutines are called
Department of Computer Science, the
University of Illinois at Chicago 6
Binding Lifetime and Key Events

Create
bindings

Create
Reference
objects

The period of time between the


creation and the destruction of a
name-to-object binding is called
Destroy the binding’s lifetime Deactivate
objects bindings

Destroy Reactivate
bindings bindings

Department of Computer Science, the


University of Illinois at Chicago 7
Questions

n Is the binding between the parameter name


and the variable that was passed as a
parameter to a subroutine has a longer
lifetime than that of the variable itself?

n Is it possible for a name-to-object binding to


have a lifetime longer than that of the object?

Department of Computer Science, the


University of Illinois at Chicago 8
Object Lifetimes

Static objects are given an absolute address that is


retained throughout the program’s execution
• Global variables are examples of static objects
• Instructions, local static variables

Stack objects are allocated in the LIFO order


• Usually in conjuction with subroutine calls and returns

Heap objects may be allocated and deallocated at any


time
• Requires more general and expensive storage management

Department of Computer Science, the


University of Illinois at Chicago 9
Local Variables

n Local variables are created when their


subroutines are called and destroyed when
these subroutines return
q Repeatedly calling a subroutine creates and
destroys separate instances of its local variables
q What about recursion?

n Compile time constants are assigned values


that are determined at compile time
Department of Computer Science, the
University of Illinois at Chicago 10
Program Memory Segmentation
n Text
q It is a code segment that contains program instructions. EIP
points to an instruction to be executed
n Data
q Stores initialized global program variables
n BSS (Block Started by Symbol)
q Stores static variables that are filled solely with zero-valued data
initially
n Heap
q Stores program variables and can grow and shrink in size. It
grows toward higher memory addresses
n Stack
q LIFO temporary storage that’s stores context during subroutine
calls. Push and Pop are two basic operations for the stack. It
grows toward lower memory addresses

11
Memory Layout
Higher
Address

STACK

HEAP
Lower

CODE Address

12
Stack-Based Allocation

n Each instance of a subroutine at run time has


its own frame (activation record)
q Contains arguments, return values, local vars, etc
n Arguments and return values lie at the bottom
of the frame
q Easier for a caller to access them
n A stack is maintained by a subroutine called
calling sequence. It consists of
q Prologue (code executed at the beginning)
q Epilogue (code executed at the end)
13
Stack-Based Allocation
sp
Lower
Address
Temporaries
fp Subroutine D

Stack grows this way


Local variables
Subroutine C

Bookkeeping
Subroutine B
fp when
Return address C is running
Subroutine A
Arguments
called from the
and
main program Higher
returns Address

14
Stack-Based Allocation

n Location of a stack frame cannot be predicted at


compile time
q Because we do not know what other frames may already
be on the stack
n The offset of objects within a frame can be
determined statically
n Frame pointer (fp) points to some known location
within the frame of the currently executing
subroutine
n The stack pointer contains the address of the first
unused location at the top of the stack

15
Stack Operations
n Code is executed until function call F is encountered
n Push the arguments for F on the stack
n Call F and place its return address RET on the stack
q RET is the address stored in the instruction pointer EIP at the
time when F is called
q It is the location at which to continue the execution when F is
completed
n Execute the prolog code
q EBP (fp) value is pushed onto the stack

q Copy ESP to EBP

n Calculate the size of the space required to keep local variables


n Push local variables to the stack
n We are done!

16
Example

void function(int a, int b)


{ sp

AAAAAAAAAA
int array[5]; AAAAAAAAAA
array
AAAAAAAAAA
}
AAAAAAAAAA
EBP
int main()
AAAAAAA
RET
{
b
function(1, 2);
a
}
17
Program Exploit: Buffer Overflow

n Look at this C code example


1. int main( int argc, char *argv[] ) {
2. char buffer[100];
3. int i;
4. for( i = 0; i < 150; i++ ) {
5. buffer[i] = ‘A’;
6. }
7. }
n What happens when we compile and run
this program?
18
Heap-Based Allocations

A heap is a region of storage in which


subblocks are allocated and
deallocated at arbitrary times

Heaps are required for objects whose


sizes may change as a results of
different operations

Department of Computer Science, the


University of Illinois at Chicago 19
What Is the Value of sum?

int j = 0, sum = 0;

for(int i = 1; i < 100; i++){


j = j++*i;
sum += j;
}

System.out.println(sum);
Department of Computer Science, the
University of Illinois at Chicago 20
Java Language Specification
n Version Java SE X Edition
q https://docs.oracle.com/en/java/javase/X
q https://docs.oracle.com/javase/specs/jls/seX/html/index.html
n Section 15.14 https://docs.oracle.com/javase/specs/jls/seX/html/jls-
15.html#jls-15.14
n At run time, if evaluation of the operand expression completes abruptly, then
the postfix increment expression completes abruptly for the same reason
and no incrementation occurs. Otherwise, the value 1 is added to the value
of the variable and the sum is stored back into the variable.
n Before the addition, binary numeric promotion (§5.6.2) is performed on the
value 1 and the value of the variable. If necessary, the sum is narrowed by a
narrowing primitive conversion (§5.1.3) and/or subjected to boxing
conversion (§5.1.7) to the type of the variable before it is stored.
n The value of the postfix increment expression is the value of the variable
before the new value is stored.

Department of Computer Science, the


University of Illinois at Chicago 21
What Is the Value of sum?

int j = 0, sum = 0;

for(int i = 1; i < 100; i++){


j = j++*i;
sum += j;
}

System.out.println(sum);
Department of Computer Science, the
University of Illinois at Chicago 22
What About This Program?

public class Null {


public static void greet( ) {
System.out.println("Hello world!");
}

public static void main(String[ ] args) {


((Null) null).greet( );
}
}

Department of Computer Science, the


University of Illinois at Chicago 23
null
n 3.10.7 The Null Literal
q The null type has one value, the null reference, represented by the null
literal null, which is formed from ASCII characters.
q A null literal is always of the null type (§4.1)
n There are two kinds of types in the Java programming language:
primitive types(§4.2) and reference types (§4.3).
n There is also a special null type, the type of the expression null (
§3.10.7, §15.8.1),which has no name.
n Because the null type has no name, it is impossible to declare a
variable of the null type or to cast to the null type.
n The null reference is the only possible value of an expression of null
type.
n The null reference can always be assigned or cast to any reference
type (§5.2, §5.3,§5.5).
Department of Computer Science, the
University of Illinois at Chicago 24
Null
n An identifier cannot have the same spelling (Unicode
character sequence) as a keyword (§3.9), boolean literal
(§3.10.3), or the null literal (§3.10.7), or a compile time
error occurs.
n Two identifiers are the same only if, after ignoring
characters that are ignorable, the identifiers have the
same Unicode character for each letter or digit.
q An ignorable character is a character for which the method
Character.isIdentifierIgnorable(int) returns true. Identifiers that
have the same external appearance may yet be different.
n Keywords are 51 character sequences, formed from
ASCII letters, are reserved for use as keywords and
cannot be used as identifiers (§3.8).
Department of Computer Science, the
University of Illinois at Chicago 25
JLS Again

n Section 8.4.3.2. static Methods


n A method that is declared static is called a class method.
n It is a compile-time error to use the name of a type parameter of any
surrounding declaration in the header or body of a class method.
n A class method is always invoked without reference to a particular
object. It is a compile-time error to attempt to reference the current
object using the keyword this (§15.8.3) or the keyword super
(§15.11.2).
n A method that is not declared static is called an instance method,
and sometimes called a non-static method.
n An instance method is always invoked with respect to an object,
which becomes the current object to which the keywords this and
super refer during execution of the method body.

Department of Computer Science, the


University of Illinois at Chicago 26
Reading

n Mandatory
q The Michael Scott book, chapter 3
q The Scala book, chapters 1-3

n Optional for undergrads, mandatory for


graduate students
q Max Schäfer, Torbjörn Ekman, Oege de
Moor: Sound and extensible renaming for
java. OOPSLA 2008: 277-294.

Department of Computer Science, the


University of Illinois at Chicago 27

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy