Variables and values Value (expression) calculation Data elements - - PowerPoint PPT Presentation

variables and values value expression calculation data
SMART_READER_LITE
LIVE PREVIEW

Variables and values Value (expression) calculation Data elements - - PowerPoint PPT Presentation

Variables and values Value (expression) calculation Data elements Variable and type binding and bindings Static and dynamic binding (=variables) Variable lifetime and memory management Variable scope Static and


slide-1
SLIDE 1

121

Data elements and bindings (=variables)

  • Variables and values
  • Value (expression) calculation
  • Variable and type binding

– Static and dynamic binding

  • Variable lifetime and memory

management

  • Variable scope

– Static and dynamic scope

slide-2
SLIDE 2

122

Data elements (variables)

  • Essential operations of the program

– Memory location = place for data item – Content of the location = value of the data item

  • pointer-/reference type
  • Value of a data item is a memory address or a

reference to a memory location

  • In most languages identifies a variable

(pointers equal → the same variable)

  • In some languages "name" (of a variable)

is a data element, in some, a reference to a data element (or both of them are possible)

slide-3
SLIDE 3

123

Variables

  • The following characteristics:

– name (permanent or changeable) – memory address (L-value) ("variable's identity") – value (R-value) – lifetime (memory management) – scope (visibility) – type

  • aliasing

– Several names refer

to same L-value

– (In C++ pointers also

can cause aliasing)

R: X <- X + 1; Scala: var X: Int = value; Java: // ... void f(int i1, Integer i2) { i1 = i1 + i2; i2 = i2 + i1; } // ... x.f(1, new Integer(2));

slide-4
SLIDE 4

124

Value vs. reference semantics

  • In some languages (C, C++, ...) variables

contain the data ("value semantics")

  • In other languages (Java, Python, ....) variables

(= names) are references to objects that contain the data ("reference semantics")

  • In value semantics, a name is permanently tied

to a data item (whose value can change)

  • In reference semantics, a name can be

changed (rebound) to refer to another data

  • Affects variable lifetime, memory allocation etc.
  • A major source of confusion when changing to

another language

slide-5
SLIDE 5

125

Value semantics

  • Variables: named data items
  • (Usually also nameless variables

exist)

  • Assigning to a variable changes the

data

  • Pointers/references: variables that

refer to another variable

3 a p

slide-6
SLIDE 6

126

Reference semantics

  • Objects: nameless data items
  • Variables/references/names (term

depends on language): named references to objects

  • (Usually also nameless references exist)
  • Assigning to a variable changes the

reference (refers now to another object)

  • Objects and variables separate,

variables cannot refer to other variables,

  • nly objects

a 3

slide-7
SLIDE 7

127

Assignment

  • Changes the state of something

L = R (or L := R, or L <- R)

  • L must be an L-value, and R must be a R-value
  • Value semantics: Data element identified by L

is changed to contain value of R

  • Reference semantics: Name L is changed to

refer to R instead of its original "value"

  • Type checking: Make sure R is compatible with

the type of L

  • (In value semantics, L can usually also be a

result of computation i.e., an expression)

slide-8
SLIDE 8

128

Lifetime

  • Variable bound to memory

– Memory allocation and deallocation

  • Variable lifetime

– Time during which a variable is bound to a specific

memory location and its value is well-defined

– Binding between data item and a memory location – Usually determined at compile time

  • Exceptions such as persistent data in database
  • Lifetime may be different from memory allocation

– For local variables memory is often allocated at the

beginning of function, even if initialization is later

– E.g., C/C++ variable may be created to an already

allocated memory location (placement new)

slide-9
SLIDE 9

129

Variable classification based on lifetime and memory allocation

  • Static variables

– Bound before execution to a specific memory location – Location is static throughout the execution – Global variables, C/C++ ”static” variables. – Amount of memory known at compile time (= type has to be known, if not a reference)

  • Stack-dynamic variables

– Binding happens when function is entered – Memory reserved from execution stack – Amount of memory known at compile time (= type has to be known, if not a reference) – C++ local variables, Java local basic type variables and references

slide-10
SLIDE 10

130

Variable classification based on lifetime and memory allocation

  • Explicit heap-dynamic variables

– Binding to a specific memory location is done by programmer's explicit request – Variable is accesses through pointer/reference – Variable has no name – Type is typically bound at compile time – C++ new & delete, Java objects (new) – Sometimes automatic (Java autoboxing)

  • Implicit heap-dynamic variables

– Binding happens upon assignment – Re-assignment causes a new binding and types may change – E.g., variables in most script languages (python etc)