Pointers and memory · core
Segmentation faults and finding memory bugs
A segmentation fault means the program touched memory it may not use. In coding tests it is usually an index past the end, a null or dangling pointer, or a stack that ran out. Tools find the line quickly.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- SIGSEGV (segmentation fault): the program read or wrote memory it is not allowed to use: past the end of an array, through a null or dangling pointer, or past the end of the stack.
- Undefined behaviour means the standard puts no limit on what happens. The same bug can work on your laptop and crash on the judge, or change with -O2.
- Out of range: an array of 1,00,000 places when n can be 10,00,000. Size arrays from the constraints, or use
vector<int> a(n). v[i] does not check the index; v.at(i) throws std::out_of_range. - Stack overflow: recursion as deep as n (a DFS on a chain of 10^6 nodes), or a huge local array. Make big arrays global or use a vector (its data is on the heap), and turn very deep recursion into a loop.
- Recursion needs a base case and must move toward it; without one the stack runs out. Each call has its own frame: code before the recursive call runs on the way down, code after it on the way back up.
- Find the line: build with
g++ -g -fsanitize=address,undefinedand run the failing input. AddressSanitizer reports out-of-bounds access and use after free; UndefinedBehaviorSanitizer reports signed overflow. - In gdb:
gdb ./main, thenrun, thenbtafter the crash shows the chain of calls that led to it. - Before you submit: test the largest input and the edge cases (n = 1, all values equal). Do not just resubmit, and do not make every array 10^9 long.
The red pen
Common traps: mistake → fix
Mistake: It works on my laptop, so the judge is wrong
Fix: undefined behaviour can work by luck on one machine.
Mistake: A bigger array like 10^9 fixes the crash
Fix: it may not fit in memory; size it from the constraints.
Mistake: v[i] checks the index
Fix: only v.at(i) does.
Mistake: Recursion is always fine for n = 10^6
Fix: the stack is small, and deep recursion can overflow it.
Easy to hard
Questions interviewers ask, easy to hard
- What does SIGSEGV mean? What usually causes it?
- What is undefined behaviour? Why can a bug pass on your laptop and fail on the judge?
- What does fun(3) print, where fun prints n, calls fun(n - 1), then prints n again? How many frames are on the stack at the deepest point?
- What happens if a recursive function has no base case?
- Your solution passes the samples but crashes on a hidden test with n = 10 lakh. How do you find the problem?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.