Understanding memory allocation and object lifetime in C++
This is a foundational C++ question that tests your grasp of how the language handles memory allocation, scope, and object construction across different storage classes. It's the kind of question interviewers use to separate candidates who have built mental models of C++ semantics from those who merely memorize syntax.
Working through problems like this requires you to trace the lifecycle of objects—where they live in memory (stack vs. heap), when constructors and destructors run, and how different declaration patterns affect visibility and lifetime. Getting the answer right means reasoning carefully about scope rules, default initialization, and the order of operations during program execution.
- Stack vs. heap allocation
- Object construction and destruction
- Scope and variable lifetime
- Storage class specifiers