Memory & Pointer Interview Questions
Must-know answers
Q: new vs malloc?
new / delete | malloc / free |
|---|---|
Operator, type-safe, returns T* | Function, returns void* |
| Calls constructor/destructor | Raw bytes only |
Throws std::bad_alloc on failure | Returns NULL |
| Size computed automatically | You pass the byte count |
Never mix them (delete on malloc'd memory = UB). |
Q: Stack vs heap? Automatic, fast, limited, scoped vs manual/managed, larger, lives until freed.
Q: Memory leak, dangling pointer, double free, wild pointer? (Unit 7/11)
Q: unique_ptr vs shared_ptr vs weak_ptr? Single owner / shared ownership with reference counting / non-owning observer that breaks cycles.
Q: Reference vs pointer? References can't be null or re-seated and need initialization.
Q: What is RAII? Tie resource lifetime to object lifetime; release in the destructor.
Q: What does std::move do? Casts to an rvalue reference so the object can be moved from.
Classic coding questions
- Reverse a linked list — three pointers, O(n), O(1) space
- Detect a cycle — Floyd's slow/fast pointers
- Find the middle — slow/fast pointers
- Merge two sorted lists — dummy head
- Implement a smart pointer / string class — Rule of Five
- Swap without temp (and why not to)
Using std::unique_ptr<Node> for next (Unit 18.2) is great in production; interviewers often expect raw pointers for these puzzles — but clean them up!
Example
#include <cstdlib>
#include <iostream>
#include <memory>
#include <new>
struct Node {
int val;
Node* next;
};
Node* build(std::initializer_list<int> vals) {
Node* head = nullptr;
Node** tail = &head;
for (int v : vals) { *tail = new Node{ v, nullptr }; tail = &(*tail)->next; }
return head;
}
void print(const Node* h) { for (; h; h = h->next) std::cout << h->val << ' '; std::cout << '\n'; }
void destroy(Node* h) { while (h) { Node* n = h->next; delete h; h = n; } }
Node* reverse(Node* head) {
Node* prev = nullptr;
while (head) { Node* next = head->next; head->next = prev; prev = head; head = next; }
return prev;
}
Node* middle(Node* head) {
Node *slow = head, *fast = head;
while (fast && fast->next) { slow = slow->next; fast = fast->next->next; }
return slow;
}
bool has_cycle(Node* head) {
Node *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
Node* merge(Node* a, Node* b) {
Node dummy{ 0, nullptr };
Node* t = &dummy;
while (a && b) {
if (a->val <= b->val) { t->next = a; a = a->next; } else { t->next = b; b = b->next; }
t = t->next;
}
t->next = a ? a : b;
return dummy.next;
}
struct Widget {
Widget() { std::cout << " Widget constructed\n"; }
~Widget() { std::cout << " Widget destroyed\n"; }
};
int main() {
std::cout << "new vs malloc:\n";
Widget* w = new Widget; // constructor runs
delete w; // destructor runs
void* raw = std::malloc(sizeof(Widget)); // just bytes: no constructor
std::cout << " malloc gave " << (raw ? "raw memory, no constructor call" : "NULL") << '\n';
std::free(raw);
Node* list = build({ 1, 2, 3, 4, 5 });
std::cout << "middle: " << middle(list)->val << '\n';
list = reverse(list);
std::cout << "reversed: ";
print(list);
std::cout << std::boolalpha << "has cycle? " << has_cycle(list);
Node* last = list;
while (last->next) last = last->next;
last->next = list->next; // create a cycle
std::cout << ", after linking tail back: " << has_cycle(list) << '\n';
last->next = nullptr; // break it before freeing
destroy(list);
Node* merged = merge(build({ 1, 4, 9 }), build({ 2, 3, 10 }));
std::cout << "merged: ";
print(merged);
destroy(merged);
std::cout << "smart pointers:\n";
auto u = std::make_unique<Widget>();
auto s1 = std::make_shared<int>(7);
std::weak_ptr<int> wk = s1;
{
auto s2 = s1;
std::cout << " shared count " << s1.use_count() << '\n';
}
s1.reset();
std::cout << " weak_ptr expired after last owner reset? " << wk.expired() << '\n';
try {
[[maybe_unused]] auto* huge = new long long[static_cast<std::size_t>(-1) / 16];
} catch (const std::bad_alloc&) {
std::cout << " impossible allocation threw std::bad_alloc\n";
}
return 0;
}
/* Output:
new vs malloc:
Widget constructed
Widget destroyed
malloc gave raw memory, no constructor call
middle: 3
reversed: 5 4 3 2 1
has cycle? false, after linking tail back: true
merged: 1 2 3 4 9 10
smart pointers:
Widget constructed
shared count 2
weak_ptr expired after last owner reset? true
impossible allocation threw std::bad_alloc
Widget destroyed
*/