Week 2 Imperative Language Flashcards
(18 cards)
Variables (properties)
Name: String of characters
Type: (data type) represents the range of values that are allowed and the operations available
Address: memory address to which the variable is associated (l-value)
Value: contents of the memory location associated with the variable (r value)
Lifetime: The time in which the variable is allocated a specific memory location
Lifetime
time variable associated with specific memory address
in short- time between memory allocation and deallocation
Untyped
There is no type checking at all and hence the language enforces no type constraints
something like “hello” + 1 would pass without failure
Statically typed
Type checked before execution (at compile time ) to ensure type constraints are met
Dynamically typed
Type checked at runtime to ensure type constraints are met
Static Variable (lifetime)
. memory allocated before program execution (at compile time) and deallocated after program finishes executing
. memory reserved with that value for program duration -> cannot reuse storage
Advantage-
Effcient - memory addressing resolved at compile time
No allocation / deallocation at runtime
disadvantage:
cant reuse storage , not suitable for recursion which requires dynamic variables
Stack dynamic variable (lifetime)
eg local variables of recursive procedure
memory allocated when declaration of variable processed at run time
vital extra info ( but main bit was at top)
memory deallocated when function that declared them finishes execution
each function call gets own memory (so local variables inside it doesnt interfere with other function calls)
This why recursion works -> each function call gets fresh memory for its variables allowing states to be tracked independently
Why does recursion require stack dynamic variables (why cant we use static variables)
Number of calls is unknown in advance so we cant assign memory in advance (at compile time) because we dont know how many calls aka how much memory will be required
Furthermore static variables persists across function calls so it would lead to interference
Explicit heap dynamic variables (lifetime)
(nameless) memory cells allocated and deallocated by program instructions using pointers
eg :
int * intnode = new int // memory allocated on heap
.
.
.
In languages like c++ we must manually deallocate when out of scope (using delete keyword to prevent memory leaks)
eg: delete int node
Java -> objects are explict heap dynamic -> reference variables used to access them -> automatic dealloacation when object goes out of scope by java’s implicit garbage collection
implicit heap dynamic (lifetime)
memory allocated when variable assigned value
(IMPORTANT NOTE VARIABLES CREATED AT RUNTIME WITHOUT EXPLICIT DECLARATION) important to understand for advantages and disadvantages
advantages:
flexibility( variable types can change during runtime for variable - no explicit declaration and hence type)
disadvantage:
difficult error detection - variables created at runtime without explicit declaration so something like a typo program assumes its just a new variable you are creating and not an error
Scope
Range of Instructions in which variable visible
local - variable declared in specific block of code we are looking at
global - variable declared outside specific block of code we are looking at but it is still visible
languages usually have either:
static scoping rules
dynamic scoping rules
Static scope
scope determined at compile time
when variable referenced compiler looks for declaration in same block and if not there looks in parent block and …
if no declaration found compiler detects error
Dynamic scope
determined by the calling sequence of subprograms
when function references a variable first you look at the local variables and if declaration not there you look at function that called it -> if not there look at function that called that … until you find a matching declaration
Disadvantage:
No static type checking - as the actual variable being reference ( which we know the type from ) is determined at runtime
. program becomes hard to understand
Just read
Programs in imperative are sequences of instructions executed in order UNLESS CONTROL STATEMENTS used
What are the 3 control statements?
Selection construct( if else)
Looping construct ( while , for)
branching construct (go to )
pre test loop
test for condition done before body of loop executed
post test loop
test for condition done after body of loop executed (hence will always run at least once )