# CMPSC 331 - Exam Review & Pointer Guide

## Need-to-Know Information to Do Well on the Exam

### 1. Pointers & Memory Allocation

#### What is a Pointer?

A pointer is a variable that stores the memory address of another variable.

#### Declaring and Using Pointers

```cpp

int a = 10;

int *p = &a; // p stores the address of a

cout << *p; // Dereferencing: prints 10

```

#### Dynamic Memory Allocation

- `malloc()` (C-style) – Allocates memory but does not initialize.

- `new` (C++ style) – Allocates and initializes memory.

```cpp

int *ptr = new int(5); // Allocates memory for an int, initialized to 5

delete ptr; // Frees memory

```

#### Common Mistakes

- Memory Leaks: Forgetting to delete allocated memory.

- Dangling Pointers: Deleting memory but still using the pointer.

- Null Pointers: Always check if memory allocation was successful.

### 2. Function Pointers & Pointers to Arrays

#### Function Pointers

A function pointer stores the address of a function.

```cpp

int add(int a, int b) {

return a + b;

}

// Declare a function pointer

int (*funcPtr)(int, int) = add;

// Call function using pointer

cout << funcPtr(3, 4); // Output: 7

```

#### Pointers to Arrays

```cpp

int arr[] = {1, 2, 3};

int *ptr = arr; // Points to first element

cout << *(ptr + 1); // Output: 2

```

### 3. C++ Standard Library Functions

#### Common Functions

1. String Functions (`cstring` library)

```cpp

#include <cstring>

char str1[] = "apple";

char str2[] = "banana";

cout << strcmp(str1, str2); // Output: Negative value (since "apple" < "banana")

```

2. Memory Management (`cstdlib` library)

```cpp

#include <cstdlib>

void *ptr = malloc(10); // Allocates 10 bytes

free(ptr); // Frees memory

```

3. Mathematical Functions (`cmath` library)

```cpp

#include <cmath>

cout << pow(2, 3); // Output: 8

cout << sqrt(16); // Output: 4

```

### 4. Design Patterns

#### Adapter Pattern vs. Observer Pattern

##### Adapter Pattern (Real-World Example)

- You buy a new phone charger but your socket doesn’t match.

- You use an adapter to connect the old socket with the new charger.

##### Observer Pattern (Real-World Example)

- You subscribe to a YouTube channel.

- When a new video is uploaded, all subscribers are notified.

### 5. Code Execution & Debugging

#### Pointer Arithmetic

```cpp

int arr[] = {10, 20, 30, 40};

int *ptr = arr; // Points to arr[0]

cout << *(ptr + 1); // Output: 20

cout << *(ptr + 2); // Output: 30

```

---

## Understanding Pointers with Real-World Comparisons

| Concept | Real-World Comparison |

|------------|----------------------|

| Pointer | House Address 📍 |

| Dereferencing | Visiting the house 🚪 |

| Memory Allocation (`malloc`) | Booking a hotel room 🏨 |

| Memory Leak | Forgetting to return a rental car 🚗 |

| Dangling Pointer | Calling a disconnected phone |

| Null Pointer | Empty parking spot 🚗 |

| Pointer Arithmetic | Moving train seats 🚆 |

### 1. When is using a pointer necessary?

#### Real-World Example:

Think of a pointer like a house address.

- If you give someone your house address, they can access your home.

- If you give them a copy of your home, they can only make changes to their copy, and it won't affect your original house.

### 2. Why do we use pointers?

#### Real-World Example:

A pointer is like a remote control for a TV.

- The TV (variable) has its location.

- The remote (pointer) allows you to control the TV without physically touching it.

### 3. What is the point of dereferencing?

Dereferencing a pointer accesses the value stored at the memory address it points to.

#### Real-World Example:

- A pointer is like an address on a map.

- Dereferencing is like visiting the location instead of just knowing where it is.

### 4. Understanding Memory Leaks, Dangling, and Null Pointers

#### Memory Leak:

- When you allocate memory but forget to free it.

#### Real-World Example:

Forgetting to return a rented car.

- You rent a car (allocate memory).

- You drive it but forget to return it (forget free()).

- The rental agency (memory) loses a car (memory leak).

#### Dangling Pointer:

- A pointer that still holds the address of freed memory.

#### Real-World Example:

Calling a disconnected phone.

- You delete a number but try to call it again.

#### Null Pointer:

- A pointer that intentionally doesn’t point anywhere.

#### Real-World Example:

An empty parking spot.

- You set the pointer to NULL to indicate no valid data.

### 5. What does pointer arithmetic do?

#### Real-World Example:

Think of an array like train seats.

- ptr + 1 moves to the next seat.

- ptr + 2 moves two seats ahead.

#### Example in Code:

```c

int arr[] = {10, 20, 30, 40};

int *p = arr; // Points to arr[0]

printf("%d", *(p + 1)); // Moves to arr[1], prints 20

printf("%d", *(p + 2)); // Moves to arr[2], prints 30

```

---

Would you like practice problems to reinforce these concepts? 🚀

robot