Q1
Why is sizeof(arr) different inside main() vs inside a function void fn(int arr[])?
Inside main(), arr retains its full array type (sizeof gives total bytes, e.g. 40). When passed to a function, arrays decay into a pointer (int*), so sizeof(arr) inside fn() returns only the pointer size (8 bytes on 64-bit)—always pass length explicitly.
Q2
What causes Undefined Behavior in int *p; *p = 10; or returning the address of a local stack variable?
An uninitialized pointer points to arbitrary garbage memory, and local stack frames are reclaimed the moment a function returns. Writing through wild or dangling pointers corrupts adjacent stack variables or triggers segmentation faults.
Q3
What is C++'s Rule of Three / Rule of Five—and why can omitting a custom copy constructor crash on delete?
If a class manages raw heap memory via a pointer, the compiler-generated shallow copy copies the pointer address. Both objects call delete in their destructors on the same address—triggering a double-free crash unless you implement deep copy/move semantics or use std::unique_ptr.
Q4
Why must a C++ base class with virtual methods ALWAYS declare a virtual destructor?
Deleting a derived object through a Base* pointer without a virtual base destructor calls only ~Base(), skipping ~Derived() and leaking every derived resource.
Q5
What happens when std::vector::push_back() exceeds capacity while an iterator or pointer references an element?
std::vector allocates a larger contiguous buffer, moves/copies elements, and frees the old array—silently invalidating ALL existing iterators, pointers, and references to elements inside that vector.
Q6
Why does std::move(x) NOT actually move anything at runtime by itself?
std::move is purely a compile-time static_cast<T&&> converting an lvalue into an rvalue reference. Actual moving only occurs if the target type defines a move constructor or move assignment operator.
Q7
What subtle infinite loop happens in C/C++ if you iterate backwards using for (size_t i = n - 1; i >= 0; i--)?
size_t is unsigned. When i reaches 0 and i-- executes, it underflows to SIZE_MAX (~1.8e19), so i >= 0 stays true forever! Use for (size_t i = n; i-- > 0; ) instead.
Q8
Why does struct padding increase sizeof(struct) even when fields are smaller?
CPUs read aligned words efficiently, so compilers insert hidden alignment padding bytes between members (e.g., char followed by int pads 3 bytes). Ordering struct members from largest alignment to smallest cuts wasted memory.
Q9
What is the difference between const int* p, int* const p, and const int* const p?
const int* p is a mutable pointer to read-only data; int* const p is a fixed pointer to mutable data; const int* const p freezes both the pointer address and the target value.
Q10
Why can std::shared_ptr cyclic references (A -> B -> A) still leak memory?
If A holds a shared_ptr to B and B holds a shared_ptr back to A, neither reference count ever reaches 0. Break parent/back-pointer cycles using std::weak_ptr.