variables and values value expression calculation data
play

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


  1. ● 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 dynamic scope 121

  2. ● Essential operations of the program – Memory location = place for data item Data – Content of the location = value of the data elements item ● pointer-/reference type (variables) • 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) 122

  3. ● The following characteristics: – name (permanent or changeable) Variables – memory address (L-value) R: X <- X + 1; ("variable's identity") Scala: var X: Int = value; – value (R-value) – lifetime (memory management) – scope (visibility) Java: – type // ... ● aliasing void f(int i1, Integer i2) { – Several names refer i1 = i1 + i2; to same L-value i2 = i2 + i1; – (In C++ pointers also } // ... can cause aliasing) x.f(1, new Integer(2)); 123

  4. ● In some languages (C, C++, ...) variables contain the data ("value semantics") Value vs. ● In other languages (Java, Python, ....) variables reference (= names) are references to objects that semantics 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 124

  5. ● Variables: named data items ● (Usually also nameless variables Value semantics exist) ● Assigning to a variable changes the data ● Pointers/references: variables that refer to another variable p a 3 125

  6. ● Objects: nameless data items ● Variables/references/names (term Reference depends on language): named semantics references to objects ● (Usually also nameless references exist) ● Assigning to a variable changes the reference (refers now to another object) a ● Objects and variables separate, variables cannot refer to other variables, 3 only objects 126

  7. ● Changes the state of something L = R (or L := R , or L <- R ) Assignment ● 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) 127

  8. ● Variable bound to memory – Memory allocation and deallocation Lifetime ● 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) 128

  9. ● Static variables – Bound before execution to a specific memory Variable location classification – Location is static throughout the execution – Global variables, C/C++ ”static” variables. based on – Amount of memory known at compile time (= lifetime and type has to be known, if not a reference) memory ● Stack-dynamic variables allocation – 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 129

  10. ● Explicit heap-dynamic variables – Binding to a specific memory location is done by Variable programmer's explicit request classification – Variable is accesses through pointer/reference – Variable has no name based on – Type is typically bound at compile time lifetime and – C++ new & delete, Java objects (new) memory – Sometimes automatic (Java autoboxing) ● Implicit heap-dynamic variables allocation – Binding happens upon assignment – Re-assignment causes a new binding and types may change – E.g., variables in most script languages (python etc) 130

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend