Why do object types require dynamic memory and do primitive types need static memory?

advertisements

I was reading on memory Allocation concepts. where I found a statement saying Object Types needs Dynamic Memory and Primitive types needs Static memory they justify it saying If the requirement is of dynamic memory, it’s allocated on the heap or else it goes on a stack.

i am not getting the concept why this is so?

Every help is appreciated.

Thanks


This answer is not 100% complete, but some of this is relevant, and I don't have the rep to simply comment...

The heap sticks around for the life of the program and is much larger (in general), so it may be a design decision that objects (in memory managed languages like C# and Java) are allocated to the heap to not overload stack memory. Also, stack memory is associated with variable scope (when it can be used) by nature of a stack, and if the memory manager will handle the objects it is more efficient to do it at certain times, versus every time the variable pops off the stack. Heaps also do not have variable size restrictions, which more realistically accommodates objects, which can be very large depending on what is defined in their class.

Here is a very good explanation of the differences of the two (stack, heap):
http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx

Also very good, but more geared toward C:
http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html

Though neither of those links specifically address WHY for C#. I'm probably going to get docked for this answer :)