CS 280 Lesson 7: Variables Binding and Scope

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/34

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 9:22 PM on 4/4/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

35 Terms

1
New cards

cross-compilation

compiling on one machine to run on another

2
New cards

what are identifiers?

named things like subprograms, variables, classes, constants, etc; cannot be reserved words.

3
New cards

are names case sensitive in c++

yes

4
New cards

what are the 6 attributes of variables

name, address, value, type, lifetime, and scope

  1. not every var has a name; i.e. dynamic vars don’t have names, but pointers that refer to them

5
New cards

what are the different types of regions that memory for running a program is divided into

code, data, heap, and stack

6
New cards

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

7
New cards

memory: what does the stack do? what’s in there? what happens at the end of program?

used dynamically at runtime.

  1. supports function calls and returns

  2. variables defined INSIDE functions live here (local vars). arguments/parameters passed into a function also live here.

  3. at the end, stack manages itself automatically, and pops and destroy everything.

8
New cards

memory: what does the heap do?

  1. why is it disorganized?

  2. what does new do?

used dynamically at runtime. holds memory that is dynamically allocated and freed as needed as the program runs

  1. it’s unpredictable how programmer will dynamically allocate and free memory

  2. it requests memory from the heap

9
New cards

how do memory allocation for objects differ between C++ and Java

obviously, the memory it takes depends on the type of data. however,

  1. C++: object is created there and then, and memory is allocated to hold the entire object immediately and constructors are initialized

  2. Java: we do NOT create the object, but allocate enough memory to hold a reference to the object

    1. by definition/default, this is initialized to null

10
New cards

what does it mean for variables to be aliases?

they point to the same memory address

11
New cards

lvalue

  1. syntax?

  2. format def?

  3. can we get it’s address with & ?

the address of the variable, we use it when we care where we store something.

  1. left side of =, ex: x = 5

  2. identifies a non-temporary object

  3. oui

12
New cards

rvalue

  1. syntax?

  2. formal def?

  3. can we get its address with & ?

value or the contents of the variable; use when we care about the data itself

  1. right side of =, ex: x = 5

  2. identifies a temporary object not associated with a permanent address

  3. nein

13
New cards

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.

14
New cards

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

15
New cards

what are two ways in which static type bindings work?

  1. explicit declarations: tell compiler exactly what it is (in C and C++). ex. int val; or double num;

  2. implicit declarations: compiler figures out data type through default conventions

    1. in C++, we have auto. in C#, ts is var . etc. ex: auto x = 5 will make x integer type

16
New cards

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

17
New cards

storage binding and lifetimes: STATIC VARIABLES

  1. when is it bound?

  2. what is the lifetime?

  3. examples?

  1. bound to memory cells before execution begins

  2. remains bound to that memory cell throughout entire execution

  3. global variables, or local variables marked with static

18
New cards

storage binding and lifetimes: STACK-DYNAMIC

  1. when is it bound?

  2. what is the lifetime?

  3. examples?

  4. advantages and disadvantages?

  1. when we reach that line of code for execution (declared the var)

  2. from the moment it’s declared, to when the function or code block they’re in finishes up and is popped outta the stack.

  3. local vars in a function; function parameters

  4. 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)

19
New cards

storage binding and lifetimes: EXPLICIT HEAP-DYNAMIC

  1. when is it bound?

  2. what is the lifetime?

  3. examples?

  4. advantages and disadvantages?

  1. allocated and deallocated by explicit lines by programmer

  2. 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.

  3. using new and delete

  4. PLUS: gives programmer total control over dynamic storage management. MINUS: inefficient and unreliable if programmer forgor to deallocate

20
New cards

storage binding and lifetimes:

  1. when is it bound?

  2. what is the lifetime?

  3. examples?

  4. advantages and disadvantages?

  1. it’s bound to heap storage only when they’re assigned values

  2. 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

  3. javascript/php variables. ex. let myVar = 10; then myVar = [1,2]

  4. PLUS: extreme flexibility. DISADV: heavy, inefficient runtime overhead

21
New cards

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

22
New cards

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:

  1. C++ namespace

  2. Java packages

  3. Classes and nested classes

  4. Functions

  5. Blocks {…}

  6. For loops

23
New cards

what are the three kinds of variables by location for scope?

  1. local vars: declared inside specific program unit you’re looking at

  2. nonlocal var: vars visible inside the program unit you’re looking at, but were declared elsewhere

  3. global var: a special, top level category of noncal vars

24
New cards

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

25
New cards

what is static scoping

scope is determined by reading text of the code, not by how it runs

26
New cards

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

27
New cards

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.

28
New cards

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

29
New cards

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;

30
New cards

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

31
New cards

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

32
New cards

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

33
New cards

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()

  1. x = std::abs(val); directly

  2. using std::abs declaring that function, allowing us to do x = abs(y)

  3. using namespace std; opening up entire namespace locally or globally

34
New cards

what is the referencing environment of a single statement?

  1. what is ts in static scope languages?

  2. 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

  1. local vars + all vars from enclosing scopes

  2. 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

35
New cards

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.

Explore top notes

note
Railroads Notes
Updated 772d ago
0.0(0)
note
2.1 Physical and Mental Health
Updated 1118d ago
0.0(0)
note
Spansih
Updated 527d ago
0.0(0)
note
AP GOV Unit 5
Updated 936d ago
0.0(0)
note
Unit 3 The Atom >
Updated 1048d ago
0.0(0)
note
Railroads Notes
Updated 772d ago
0.0(0)
note
2.1 Physical and Mental Health
Updated 1118d ago
0.0(0)
note
Spansih
Updated 527d ago
0.0(0)
note
AP GOV Unit 5
Updated 936d ago
0.0(0)
note
Unit 3 The Atom >
Updated 1048d ago
0.0(0)

Explore top flashcards

flashcards
Latin 1 Vocab Review
210
Updated 525d ago
0.0(0)
flashcards
Biology Exam Review
98
Updated 1210d ago
0.0(0)
flashcards
Fluid and Electrolytes
24
Updated 1095d ago
0.0(0)
flashcards
APUSH Period 5
110
Updated 556d ago
0.0(0)
flashcards
English Vocab 7
31
Updated 1037d ago
0.0(0)
flashcards
Latin 1 Vocab Review
210
Updated 525d ago
0.0(0)
flashcards
Biology Exam Review
98
Updated 1210d ago
0.0(0)
flashcards
Fluid and Electrolytes
24
Updated 1095d ago
0.0(0)
flashcards
APUSH Period 5
110
Updated 556d ago
0.0(0)
flashcards
English Vocab 7
31
Updated 1037d ago
0.0(0)