What is the difference between stack and heap allocation?
Simple meaning
Stack objects die when the scope ends and are fast.
Open the full page for Why, Steps, Example and Key takeaway.
Panel-ready C++ interview set questions for freshers and experienced developers. Practice at Coding Cadre in Faridabad, or Online from Delhi NCR.
Stack objects die when the scope ends and are fast.
Open the full page for Why, Steps, Example and Key takeaway.
I mark methods and params const when they should not modify state.
Open the full page for Why, Steps, Example and Key takeaway.
Releasing resources the object owns when it goes out of scope.
Open the full page for Why, Steps, Example and Key takeaway.
A race happens when two threads use shared data without proper synchronization and the result depends on timing.
Open the full page for Why, Steps, Example and Key takeaway.
vector is dynamic size on the heap
Open the full page for Why, Steps, Example and Key takeaway.
CPUs fetch memory in cache lines.
Open the full page for Why, Steps, Example and Key takeaway.
A null-terminated const char pointer for C APIs.
Open the full page for Why, Steps, Example and Key takeaway.
Prefer types whose members manage themselves so you write no special destructor or copy ops.
Open the full page for Why, Steps, Example and Key takeaway.
A declaration introduces a name
Open the full page for Why, Steps, Example and Key takeaway.
Documenting invariants in debug builds that abort on violation.
Open the full page for Why, Steps, Example and Key takeaway.
Exclusive ownership of a heap object that deletes on destruction.
Open the full page for Why, Steps, Example and Key takeaway.
Atomic refcount updates add overhead and can bounce cache lines.
Open the full page for Why, Steps, Example and Key takeaway.
It casts to an rvalue so move constructors can steal resources.
Open the full page for Why, Steps, Example and Key takeaway.
In templates it preserves value category when passing arguments onward.
Open the full page for Why, Steps, Example and Key takeaway.
map keeps keys ordered and has stable iterators references more often
Open the full page for Why, Steps, Example and Key takeaway.
Reallocation invalidates all iterators and references.
Open the full page for Why, Steps, Example and Key takeaway.
I check has_value or use value_or before value().
Open the full page for Why, Steps, Example and Key takeaway.
A data race is concurrent conflicting access without sync — UB in C++.
Open the full page for Why, Steps, Example and Key takeaway.
Mutual exclusion around a critical section sharing state.
Open the full page for Why, Steps, Example and Key takeaway.
Threads wait until a predicate becomes true and get notified.
Open the full page for Why, Steps, Example and Key takeaway.