0

Many tutorials say that reference types are created on the heap and can only be destroyed when the last reference disappears. On the contrary, value types are created on the stack and destroyed automatically right after they go out of scope. This information can be found in many literature.

But I couldn't figure out the reason why reference types are not just destroyed such as in value type case.

trincot
  • 317,000
  • 35
  • 244
  • 286
user1245
  • 107
  • 4
  • Have you read http://stackoverflow.com/q/79923/139010? – Matt Ball Mar 17 '15 at 02:33
  • Ok I know how stack and heap works. My question is WHY? What is the reason? What is the benefit not to delete heap just after as in stack? – user1245 Mar 17 '15 at 02:35
  • Given that you're asking this question it sounds like you don't fully see the difference yet. **How does the system know when it's safe to delete objects from the heap?** – Matt Ball Mar 17 '15 at 04:34

1 Answers1

0

Many tutorials say that reference types ... can only be destroyed when the last reference disappears.

How else do you know when a reference type has gone out of scope?

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • It sounds like you're imagining some alternative mechanism which would allow reference types to be trivially deallocated. Is that right? – Matt Ball Mar 17 '15 at 02:35
  • No I'm wondering why deallocation on the heap not that easy as in the stack. They are both in the RAM. – user1245 Mar 17 '15 at 02:53