1/34
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
cross-compilation
compiling on one machine to run on another
what are identifiers?
named things like subprograms, variables, classes, constants, etc; cannot be reserved words.
are names case sensitive in c++
yes
what are the 6 attributes of variables
name, address, value, type, lifetime, and scope
not every var has a name; i.e. dynamic vars don’t have names, but pointers that refer to them
what are the different types of regions that memory for running a program is divided into
code, data, heap, and stack
memory: what is stored in code and data
it holds machine code, executable compiled from your program.
it holds constants, hardcoded numeric or string values
it holds global vars
memory: what does the stack do? what’s in there? what happens at the end of program?
used dynamically at runtime.
supports function calls and returns
variables defined INSIDE functions live here (local vars). arguments/parameters passed into a function also live here.
at the end, stack manages itself automatically, and pops and destroy everything.
memory: what does the heap do?
why is it disorganized?
what does new do?
used dynamically at runtime. holds memory that is dynamically allocated and freed as needed as the program runs
it’s unpredictable how programmer will dynamically allocate and free memory
it requests memory from the heap
how do memory allocation for objects differ between C++ and Java
obviously, the memory it takes depends on the type of data. however,
C++: object is created there and then, and memory is allocated to hold the entire object immediately and constructors are initialized
Java: we do NOT create the object, but allocate enough memory to hold a reference to the object
by definition/default, this is initialized to null
what does it mean for variables to be aliases?
they point to the same memory address
lvalue
syntax?
format def?
can we get it’s address with & ?
the address of the variable, we use it when we care where we store something.
left side of =, ex: x = 5
identifies a non-temporary object
oui
rvalue
syntax?
formal def?
can we get its address with & ?
value or the contents of the variable; use when we care about the data itself
right side of =, ex: x = 5
identifies a temporary object not associated with a permanent address
nein
what is binding? binding time?
binding is the association between an attribute (like name, or type) and an entity (memory cell, or value).
binding time is the exact moment this association takes place.
what are the two kinds of bindings?
static binding: binding is locked before the program runs, and remains unchanged throughout execution
dynamic binding: binding occurs during runtime and can change in that time as well
what are two ways in which static type bindings work?
explicit declarations: tell compiler exactly what it is (in C and C++). ex. int val; or double num;
implicit declarations: compiler figures out data type through default conventions
in C++, we have auto. in C#, ts is var . etc. ex: auto x = 5 will make x integer type
how does dynamic type binding work? what are advantages and disadvantages?
type is bound when the assignment statement is executed. in python, this is literally for any variable, ex: x = 5, is dynamically bound.
advantage: highly flexible
disadvantage: high cost, and compiler has a hard time catching error before program crash
storage binding and lifetimes: STATIC VARIABLES
when is it bound?
what is the lifetime?
examples?
bound to memory cells before execution begins
remains bound to that memory cell throughout entire execution
global variables, or local variables marked with static
storage binding and lifetimes: STACK-DYNAMIC
when is it bound?
what is the lifetime?
examples?
advantages and disadvantages?
when we reach that line of code for execution (declared the var)
from the moment it’s declared, to when the function or code block they’re in finishes up and is popped outta the stack.
local vars in a function; function parameters
PLUS: allows for recursion and conserves memory since it wipes itself clean each time. MINUS: overhead; not history-sensitive (local vars don’t remember their value last time they were called)
storage binding and lifetimes: EXPLICIT HEAP-DYNAMIC
when is it bound?
what is the lifetime?
examples?
advantages and disadvantages?
allocated and deallocated by explicit lines by programmer
from the moment it’s allocated, to when we deallocate it or program garbage collector (like in Java) takes care of it. they do NOT die at the end of a function.
using new and delete
PLUS: gives programmer total control over dynamic storage management. MINUS: inefficient and unreliable if programmer forgor to deallocate
storage binding and lifetimes:
when is it bound?
what is the lifetime?
examples?
advantages and disadvantages?
it’s bound to heap storage only when they’re assigned values
from when a value is assigned, to when they’re assigned a new value (like changing data types) requiring new memory allocation, or when the garbage collector reclaims the memory if var is no longer in use
javascript/php variables. ex. let myVar = 10; then myVar = [1,2]
PLUS: extreme flexibility. DISADV: heavy, inefficient runtime overhead
for the following code, where in the memory does p and new SomeClass() live, respectively?
SomeClass *p;
p = new SomeClass();stack-dynamic, and explicit heap dynamic
what is a scope of a variable? what are some examples?
range of statements over which the variable is visible (i.e. able to be referenced or assigned a value in such statements)
examples:
C++ namespace
Java packages
Classes and nested classes
Functions
Blocks {…}
For loops
what are the three kinds of variables by location for scope?
local vars: declared inside specific program unit you’re looking at
nonlocal var: vars visible inside the program unit you’re looking at, but were declared elsewhere
global var: a special, top level category of noncal vars
what is the difference between C++ and Java in regards to global variables?
C/C++ allows global vars, but Java does not, as everything in Java must exist in a class
what is static scoping
scope is determined by reading text of the code, not by how it runs
in static scoping, how are nested subprograms handled?
it searches in this order: local → go to other levels of nesting → global . bc of this, a “closer” var hides vars that are “further away”
if it can’t find it, it gives undeclared variable error
in static scoping, how are disjoint subprograms handled and which languages do ts?
in what instance do these languages DO exhibit nested scopes?
for starters, disjoint subprograms mean you can’t make subprograms within other subprograms (ex: C++ and java don’t allow you to declare a function inside of a function).
in this case, you’re free to js use same var names without any interference.
nested scopes are exhibited by C++ only by nested class definitions and if/else or while/for loops.
NOTE: in Java, you CANNOT use variable shadowing (using same var name but for a different variable in an inner scope) for while loops, for loops, etc. Java only allows you to ACCESS variables existing outside of such loops (i.e. their scope), but not make new ones.
what is the scope of a local variable in Java / C++ (braindead)
from its declaration to when the function/code block’s closing brace } hits
for global scopes, what is extern
normally, declaring global vars obviously immediately reserves storage for it. however, global var is declared after a function, that function cannot see it.
so we use extern to tell the compiler that the var exists, but it’s elsewhere, so do NOT reserve storage for it here. we use this in that function that can’t otherwise see global var: extern int x;
recall for global and local scopes, we access closest one first (variable shadowing). what do you do if you wanna access the global one instead?
use :: (scope resolution operator) before the variable. ex: ::x
python: how do we edit global var while inside a local function
must use global keyword: global x = 4 ; otherwise ts creates or changes a local var
what is the difference between static and dynamic scoping and what languages are associated with which?
static (C/C++/Java/Python): based on textual layout of code
dynamic (Perl/SNOBOL 4/etc): based on calling sequence of program at runtime.
cop a look at this code:
function big() {
function sub1(){
var x = 7;
. . .
sub2();
}
function sub2() {
var y = x;
. . .
}
var x = 3;
...
sub1();
}with static, y = x = 3
with dynamic, y = x = 7
what is a namespace and how do we access it (3 ways)?
namespace creates a named scope. prevents naming collisions.
consider the std namespace with its functions… we tryna use abs()
x = std::abs(val); directly
using std::abs declaring that function, allowing us to do x = abs(y)
using namespace std; opening up entire namespace locally or globally
what is the referencing environment of a single statement?
what is ts in static scope languages?
what is the caveat with java’s this
complete collection of all variable names that are visible and usable at this exact line of code
local vars + all vars from enclosing scopes
in java, if a function has same parameters (esp constructors) as class’s private variables, they’ll hide the class variables. to access class variables, we do: this.variable
what is a named constant? how do different languages deal with it?
it’s a var bound to a value only once; then, it’s read-only.
C++/Java allows us to do dynamic bindings for them. but for C#, it depends. using const makes it statically bound and needs to be defined at compile time (not dynamic). however, using readonly let’s us do dynamic bindings for them.