1/18
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Describe pass by value
A variable's value is copied and passed to the function.
Changes to the parameter inside the function do not affect the original variable.
Simple and easy to implement but inefficient for large data structures because copying is involved.
Example explanation:
change(x) creates a copy of x
Modifying the copy does not affect the original variable
Describe pass by reference
Pass by reference allows a function to modify the original variable without copying it.
The parameter becomes an alias for the original variable.
Key points:
int &num → num is a reference to the original variable
Any changes to num directly affect the original variable
Output example explanation:
Original variable value changes from 10 → 50 after function call
describe pointers
Pointers store memory addresses of other variables.
&var returns the memory address of var.
Declaration: int* point_var;
Assigning addresses:
int var = 5; int* point_var = &var;
Good practice: initialize pointers when declared
describe reference
A reference variable is an alias for an existing variable.
Created with & during declaration
Changing the reference changes the original variable
Compare and contrast pointers and references
Feature | Pointer | Reference |
Definition | Stores memory address of another variable | Alias for an existing variable |
Syntax | int *p; | int &r = x; |
Initialization | Can be initialized later; can be null | Must be initialized; cannot be null |
How is a pointer different from an ordinary variable?
Ordinary variables store direct values
Pointers store memory addresses
Pointers need * to dereference and access the value
Why is pass by reference often preferred over pass by value?
Performance: avoids copying large objects (e.g., vectors, strings, custom classes)
Efficient for large data structures with dynamic memory
Describe an advantage or disadvantage of pass by value
Advantage: Simple, independent of original variable
Disadvantage: Inefficient for large objects (copies data)
Describe an advantage or disadvantage of pass by reference
Advantage: Modifies original variable without copying
Disadvantage: Changes original variable, can cause unintended side effects
Describe garbage collection
Memory recovery feature in languages like Java
Frees memory that is no longer needed, preventing memory exhaustion
C++ does not have built-in garbage collection
C++ has no mechanism for garbage collection. What does that mean for us as programmers?
Must manually manage memory
Allocate with new and free with delete
Can also use smart pointers or containers that manage memory automatically
Describe memory leaks and how they occur
Occur when dynamically allocated memory is not freed
Memory remains reserved and cannot be reused, slowing the program or exhausting memory
Describe memory fragmentation and how it occurs
Causes:
Frequent new/delete or malloc/free calls of different sizes
Types:
External fragmentation: Free memory divided into small non-adjacent blocks
Internal fragmentation: Allocated block larger than needed
Describe paging/swapping
swap() exchanges the values of two variables
Both variables must have the same data type
How, in C++, do we go about allocating and freeing up memory dynamically?
Use malloc() or calloc() from <stdlib.h>
malloc(size) → allocates memory
calloc(amount, size) → allocates multiple items, initialized to zero
Describe scope and how it affects variable visibility
Variables are only accessible inside the region they are created
Local Scope:
Variable inside a function, accessible only within that function
Global Scope:
Variable outside all functions, accessible anywhere
When two objects declared in different scopes share the same name, which takes precedence? How do we retrieve the other?
Innermost scope takes precedence → called variable shadowing
Use scope resolution operator :: to access outer/global variable
Describe default parameters. Where are they placed in a function?
Default arguments are used if no value is provided during function call
Must be on the right side
Recommended to specify in the function declaration (header)
What does a function’s type say about the function?
Specifies the type of value returned by the function
Use void if no value is returned