Pointer Class Activity

Goals

  • Understand pointers to local vs dynamic variables.
  • Use nullptr safely.
  • Allocate and free memory with new / delete.
  • See lifetime pitfalls: dangling pointers and memory leaks.
  • Implement and apply a pointer-based swap.

0) Setup (1–2 min)

  1. Form a small group with those around you.
  2. Clone the project: pointeracc
  3. Open the folder containing:
    • pointers_lifecycle_lab.cpp
    • Makefile
  4. Build and run once to confirm everything compiles: make run You should see sections titled “Demo” and “Lifetime Pitfall.”

1) Read the Base Output (1–2 min)

In demo_local_pointer() you’ll see a line:

[Base] &x=0x... x=42 px=0x... *px=42

Answer briefly:

  • What does &x represent?
  • What does px represent?
  • Why are &x and px the same?
  • What does *px print, and why?

Write your all your answers in the README.md.


2) Explore Dynamic Allocation and nullptr (1–2 min)

Skim outputs from:

  • demo_dynamic_allocation() — prints py (address) and *py (value), then deletes and sets py = nullptr.
  • demo_nullptr() — shows how to check for nullptr before dereferencing.

Answer briefly:

  • Why is it useful to set a pointer to nullptr after delete?
  • Why is dereferencing a nullptr invalid?

3) Lifetime Pitfalls (2–3 min)

A. Dangling pointer

  • The function bad_return_local() returns &local. That address becomes invalid after the function returns.
  • Do not dereference the returned pointer; just observe the printed address and explain briefly:
    • What happened to local after the function ended?
    • Why would dereferencing be unsafe?

B. Memory leak

  • The function make_leak() calls new but never delete.
  • In code comments, write how you’d fix the leak (add delete and set pointer to nullptr).

Optional (only if your instructor approves): uncomment the two “danger” lines to see what would happen—expect a crash or undefined behavior. Re-comment afterward.


4) Implement pswap (2–3 min)

Open pointers_lifecycle_lab.cpp and find:

void pswap(int* a, int* b) {
    // TODO-1: implement
    // int tmp = *a; *a = *b; *b = tmp;
}
Code language: JavaScript (javascript)

Implement the body so it swaps the values pointed to by a and b.
Then, in main():

  • Call pswap(&a, &b) for locals (look for the TODO).
  • Rebuild and run: make run
  • Confirm the printed output shows a and b swapped.

Checkpoints:

  • Before swap (locals): a=10 b=20
  • After swap (locals): a=20 b=10

Explain briefly:

  • Why do we pass &a and &b here?

5) Implement make_int and Swap Dynamics (3–4 min)

Find and complete:

int* make_int(int value) {
    // TODO-2: allocate with new and return
    // return new int(value);
    return nullptr;
}
Code language: JavaScript (javascript)

Replace the return nullptr; with return new int(value);.

In main():

  • Build dynamic ints via make_int(111) and make_int(222) (already present as pa and pb).
  • Call pswap(pa, pb) (look for the TODO).
  • Run: make run

Checkpoints:

  • Before swap (dynamic): *pa=111 *pb=222
  • After swap (dynamic): *pa=222 *pb=111

Explain briefly:

  • Why do we pass pa and pb (not &pa/&pb) when swapping dynamic values?

6) Clean Up: Avoid Leaks (1 min)

At the end of main():

  • Complete the TODO to delete the dynamic integers and set pointers to nullptr: delete pa; pa = nullptr; delete pb; pb = nullptr;
  • Rebuild and run. Program should complete normally with no leaks for these allocations.

Optional: Update make_leak() to delete the pointer it allocates.


7) Quick Wrap-Up Questions (1–2 min)

Answer briefly (in comments or on your worksheet):

  1. When is it safe to dereference a pointer?
  2. Why is returning &local from a function dangerous?
  3. What happens if you delete a pointer but forget to set it to nullptr?
  4. Define a memory leak. How do you fix one in this program?

Troubleshooting

  • Build errors: run make clean then make again.
  • Crash/segfault: you likely dereferenced an invalid pointer (dangling or nullptr). Re-comment any danger lines.
  • Nothing changes after editing: save the file, run make (not just ./plab), then make run.

Deliverables

  • A successful run showing:
    • Base demo line with &x, x, px, *px.
    • Local swap before/after.
    • Dynamic swap before/after.
    • No uncommented danger lines left in final submission.
  • Short answers to the reflection questions (comments or worksheet).

Scroll to Top