0
$\begingroup$

In object oriented programming heaps are used for to store the actual objects. Stacks are used for storing reference-variables to the objects.

What's the concrete reasons for choosing those two specific data-structures?

What benefit has a heap in the described scenario? What benefit provides a stack?

$\endgroup$

1 Answer 1

2
$\begingroup$

I am not sure if choosing stacks/heap is related with object oriented programming. The stacks are adopted by many programming languages because they are perfect fit for scoped variables, e.g. create new stack frame at the beginning of a function, and discard it when leaving. They don't always store reference variables, but actual objects. In C++, you can allocate objects on stack and they will be destroyed when the control exits the scope. While stacks are often used with scopes, which determines the variables lifetime, heaps are used for objects that live longer without predefined lifetime.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.