Goals
- Understand pointers to local vs dynamic variables.
- Use
nullptrsafely. - 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)
- Form a small group with those around you.
- Clone the project: pointeracc
- Open the folder containing:
pointers_lifecycle_lab.cppMakefile
- Build and run once to confirm everything compiles:
make runYou 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
&xrepresent? - What does
pxrepresent? - Why are
&xandpxthe same? - What does
*pxprint, 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()— printspy(address) and*py(value), then deletes and setspy = nullptr.demo_nullptr()— shows how to check fornullptrbefore dereferencing.
Answer briefly:
- Why is it useful to set a pointer to
nullptrafterdelete? - Why is dereferencing a
nullptrinvalid?
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
localafter the function ended? - Why would dereferencing be unsafe?
- What happened to
B. Memory leak
- The function
make_leak()callsnewbut neverdelete. - In code comments, write how you’d fix the leak (add
deleteand set pointer tonullptr).
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
aandbswapped.
Checkpoints:
- Before swap (locals):
a=10 b=20 - After swap (locals):
a=20 b=10
Explain briefly:
- Why do we pass
&aand&bhere?
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)andmake_int(222)(already present aspaandpb). - 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
paandpb(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):
- When is it safe to dereference a pointer?
- Why is returning
&localfrom a function dangerous? - What happens if you
deletea pointer but forget to set it tonullptr? - Define a memory leak. How do you fix one in this program?
Troubleshooting
- Build errors: run
make cleanthenmakeagain. - 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), thenmake 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.
- Base demo line with
- Short answers to the reflection questions (comments or worksheet).
