oral exam - Memory, References, and Parameters - mini

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/18

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

19 Terms

1
New cards

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

2
New cards

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:

  1. int &numnum is a reference to the original variable

  2. Any changes to num directly affect the original variable

Output example explanation:

Original variable value changes from 10 → 50 after function call

3
New cards

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

4
New cards

describe reference

  • A reference variable is an alias for an existing variable.

  • Created with & during declaration

Changing the reference changes the original variable

5
New cards

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

6
New cards

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

7
New cards

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

8
New cards

Describe an advantage or disadvantage of pass by value

  • Advantage: Simple, independent of original variable

  • Disadvantage: Inefficient for large objects (copies data)

9
New cards

Describe an advantage or disadvantage of pass by reference

  • Advantage: Modifies original variable without copying

Disadvantage: Changes original variable, can cause unintended side effects

10
New cards

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

11
New cards

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

12
New cards

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

13
New cards

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

14
New cards

Describe paging/swapping

  • swap() exchanges the values of two variables

Both variables must have the same data type

15
New cards

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

16
New cards

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

17
New cards

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

18
New cards

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)

19
New cards

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