C++ – When to Use Stack vs Heap

c++

In C++, when is it best to use the stack? When is it best to use the heap?

Best Answer

Use the stack when your variable will not be used after the current function returns. Use the heap when the data in the variable is needed beyond the lifetime of the current function.