Programming languages final

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

1/83

encourage image

There are no tags or description

Looks like no tags are added yet.

84 Terms

1
New cards

Runtime Stack (Purpose)

Used for static memory allocation, storing local variables, parameters, and return addresses.

2
New cards

Runtime Stack (Management)

Managed automatically; variables are pushed on function call and popped on exit.

3
New cards

Runtime Stack (Lifetime)

Short lifetime; variables exist only during the execution of the function.

4
New cards

Runtime Stack (Access)

Fast access because it follows a Last-In-First-Out (LIFO) order.

5
New cards

Heap (Purpose)

Used for dynamic memory allocation, storing objects persisting beyond a single function.

6
New cards

Heap (Management)

Managed manually by the programmer using functions like malloc/free or new/delete.

7
New cards

Heap (Lifetime)

Long lifetime; exists until explicitly deallocated or reclaimed by garbage collection.

8
New cards

Heap (Access)

Slower access due to more complex memory management.

9
New cards

Stack Frame

A runtime stack structure containing a function's local variables, parameters, and return address.

10
New cards

Recursion: Base Case Process

The function stops calling itself, starts returning, and stack frames are popped.

11
New cards

Recursion: Unwinding

The stack pops each frame, restoring the state of the previous function call.

12
New cards

Recursion (Disadvantage)

Deep recursion can run out of stack space, causing a stack overflow.

13
New cards

Garbage Collection (GC)

Automatic memory management that reclaims heap memory no longer in use.

14
New cards

GC: Reachability

Objects accessible from root references are considered live and not collected.

15
New cards

GC: Marking Phase

The collector starts from root references and marks all live, reachable objects.

16
New cards

GC: Sweeping Phase

Reclaiming memory from dead objects.

17
New cards

GC: Compacting Phase

The collector moves live objects together to reduce fragmentation.

18
New cards

Reference Counting GC

Reclaims an object when its count of references drops to zero.

19
New cards

Tracing GC

Traces the stack and heap to find and mark reachable objects.

20
New cards

Generational GC (Concept)

Categorizes objects by age, collecting younger objects more frequently to optimize performance.

21
New cards

Young Generation

Heap section containing newly created objects, which are collected frequently.

22
New cards

Old Generation

Heap section containing long-lived objects that survived multiple GC cycles.

23
New cards

Permanent Generation

Heap section containing metadata and class definitions.

24
New cards

Minor GC

A fast garbage collection process triggered when the young generation fills up.

25
New cards

GC: Promotion

Moving objects that survived several minor GCs to the old generation.

26
New cards

Major GC

A comprehensive garbage collection process triggered when the old generation fills up.

27
New cards

Stop-and-Copy: Semi-Spaces

The heap is divided into two equal parts: active and inactive.

28
New cards

Stop-and-Copy: Copy Phase

All live objects are copied contiguously from active to inactive space.

29
New cards

Stop-and-Copy: Flip Phase

The roles of the active and inactive spaces are swapped.

30
New cards

Stop-and-Copy (Disadvantage)

Requires twice the memory because of the two semi-spaces.

31
New cards

Pass-by-Value

Passing a copy of the argument; local changes do not affect the original.

32
New cards

Pass-by-Reference

Passing a reference to the actual variable; local changes modify the original.

33
New cards

Using Pointers (Parameter Passing)

Passing a pointer to the variable, allowing the function to modify the original.

34
New cards

Pass-by-Value (Pro/Con)

Pros: Efficiency: No need to copy large objects, saving time and memory. Flexibility: Allows functions to modify the original variable.

Cons: Performance: Copying large objects can be inefficient.

Memory Usage: Requires additional memory for the copies.

35
New cards

Pass-by-Reference (Pro/Con)

Pros: Efficiency: No need to copy large objects, saving time and memory. Flexibility: Allows functions to modify the original variable.

Cons: risks unintended side effects. Requires understanding of references and potential issues like aliasing

(meaning the same object or value has more than one variable name).

36
New cards

Aliasing

When the same object or memory location has more than one variable name.

37
New cards

Recursion: Function Calls

Each recursive call creates a separate stack frame holding its state.

38
New cards

Recursion: State Management

The stack automatically maintains the state of each recursive call.

39
New cards

Recursion: Memory Efficiency

Each stack frame contains only the information needed for its function call.

40
New cards

GC: Root References

Global and stack variables can serve as starting points for reachability tracing.

41
New cards

Reference Counting: Cyclic References

Cyclic references can prevent reference counting from reclaiming objects.

42
New cards

Stop-and-Copy: Allocation

New objects are allocated in the active space.

43
New cards

Stop-and-Copy: Stop Phase

Program execution pauses when the active space fills.

44
New cards

Stop-and-Copy: Reference Updates

References are updated when live objects are moved to the inactive space.

45
New cards

Stop-and-Copy: Pause Times

Copying live objects can cause significant pauses in program execution.

46
New cards

Stop-and-Copy: Young Generation

This method is often used where most objects are short-lived.

47
New cards

Pass-by-Value: Memory Usage

Copies require additional memory.

48
New cards

Pass-by-Reference: Complexity

References require understanding potential aliasing issues. (meaning the same object or value has more than one variable name)

49
New cards

Recursion: Recursive Case

The function calls itself on a smaller or otherwise modified problem.

50
New cards

Memory Leak

Allocated memory remains unreclaimed after the program no longer needs it.

51
New cards

Dangling Pointer

A pointer that refers to memory that has already been deallocated.

52
New cards

Heap Fragmentation

Free heap memory becomes divided into scattered blocks, complicating allocation.

53
New cards

Pass-by-Value: Object References

A copied reference can still allow modification of the referenced object.

54
New cards

Automatic memory management

Reduces the burden on developers to manually manage

memory.

55
New cards

Generational garbage collection Advantages

Efficiency: By focusing on the young generation, which is smaller and contains most short-

lived objects, minor GCs are fast and efficient.

• Reduced Pause Times: Major GCs are less frequent, reducing the overall pause times for the

application.

56
New cards

Advantages of stop and copy

Defragmentation: By copying live objects to a contiguous memory area, it eliminates

fragmentation.

• Simplicity: The algorithm is straightforward to implement.

57
New cards

Disadvantages of stop and copy

Memory Overhead: Requires twice the memory since it needs two spaces.

• Pause Times: Can introduce significant pauses in program execution during the copying

phase.

58
New cards

generational garbage collection disadvantages

Performance Overhead: Garbage collection can introduce pauses in program execution,

which might affect performance.

• Non-Deterministic: The timing of garbage collection is not predictable, which can be an

issue for real-time systems

59
New cards

Using Pointers description

A reference to the actual variable is passed to the function. Changes made to

the parameter inside the function affect the original value.

60
New cards

What does a stack store?

Local variables

Function parameters

Return addresses

61
New cards

What happens to the stack when a function is called?

A new stack record (stack frame) is pushed onto the stack.

The record contains the function's parameters, local variables, and return address.

When the function finishes, its entire stack record is popped off.

Important: Every recursive function call creates a NEW stack record, even though it's the same function.

62
New cards

What does not go on the heap?

Local variables (int x = 5) : Stack

Function parameters: Stack

Return addresses :Stack

Local reference variables (Student s) :Stack

63
New cards

What does the heap store?

Dynamically allocated objects

Dynamically allocated arrays

Data created using new or malloc()

Objects that need to persist beyond a single function

Linked lists, trees, and other dynamically allocated data structures

64
New cards

Simple explanation of GC Reference counting

Every object has a reference count.

When a reference points to an object, its count increases.

When a reference is removed, its count decreases.

When the count reaches 0, the object can be collected as garbage.

65
New cards

example of GC reference counting

Imagine Object A points to Object B, and Object B points back to Object A.

Even if nothing else points to either object, their reference counts are still 1.

Reference counting may fail to recognize them as garbage.

66
New cards

Mark and sweep GC (tracing)

Reference counting may fail to recognize them as garbage.

67
New cards

difference between Reference counting and marking + sweeping

Mark and Sweep can identify circular references that are unreachable.

68
New cards

Generational GC parts

Young Generation: Newly created objects.

Old Generation: Objects that have survived multiple garbage collection cycles.

Permanent Generation: In some implementations, stores metadata and class definitions.

69
New cards

Process of generational garbage collection

A new object is created in the Young Generation.

When the young generation fills up, a Minor GC occurs.

Objects still being used survive and may be promoted to the Old Generation.

When the old generation fills up, a Major GC occurs.

70
New cards

cdr in scheme

gets everything except the first element from a list

71
New cards

car (scheme)

gets the first element of a list

72
New cards

cons function (scheme)

adds an element to the front of a list

73
New cards

null? in scheme

checks if a list is empty

74
New cards

length function in scheme

returns the length of a list

75
New cards

list in scheme

creates a list

76
New cards

equal? function in scheme

checks whether two values are equal

77
New cards

define(

begins all scheme work

78
New cards

odd? function

checks if given is odd

79
New cards

even? function

checks if given is even

80
New cards

max

returns largest number

81
New cards

pros of mark and sweep

is always correct

82
New cards

cons of mark and sweeep

very slow, program must be paused when going through the stack and heap

83
New cards

Pros of reference counting

less time than Mark & Sweep, doesn't have to check Stack and Heap

84
New cards

cons of reference counting

doesn't find all the garbage