Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

stack vs heap allocation

Posted on 2006-07-01
3
Medium Priority
?
1,001 Views
Last Modified: 2008-02-01
Why are reference types stored in heap and value types on stack. I know that they are called reference types because they are stored in heap and there is a reference on stack, but why store in heap and not in stack in the first place?
0
Comment
Question by:srinivas_vemla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Expert Comment

by:dkloeck
ID: 17024296
http://www.albahari.com/value%20vs%20reference%20types.html

(...)When the runtime deals with a value type, it's dealing directly with its underlying data and this can be very efficient, particularly with primitive types.

With reference types, however, an object is created in memory, and then handled through a separate reference – rather like a pointer.
(...)

(...)
The stack is an orderly first-in last-out memory structure, and is used for storing value-type objects.  When a method is invoked, the CLR bookmarks the top of the stack, and then allocates memory on top of this for any value type objects (including local variables) that are created.  When the method completes, the stack is 'popped' back to the bookmark and all the value-type objects created - and their associated memory - are released.

The heap, on the other hand, can be pictured as a disorderly jumble of objects, suitable for objects managed by reference.  When a reference-type object is created, the CLR allocates a block of heap memory, creates the object, then gives us a reference to it.  While the object itself will always be created on the heap, its reference may either exist on the heap or stack - depending on whether it’s part of another heap-object, or is a local variable
(...)
0
 
LVL 10

Accepted Solution

by:
dkloeck earned 500 total points
ID: 17024297
Very nice tutorial with lots of  visuals, its better put than anyone here would have time to explain.

http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx?ArticleID=9adb0e3c-b3f6-40b5-98b5-413b6d348b91


The most fundamentally important aspect of stack and heap is dynamic memory allocation.  If you allocate an array of size 10 on the stack and then another of the same size they might be allocated in the same sequence on the stack.  when the happens how do the grow the first array?? You cant.  When you allocate on the heap you have a pointer to a place in memory.  If you need more memory you allocate it and are given a pointer to it to keep track of it.
0
 

Author Comment

by:srinivas_vemla
ID: 17024867
Thanks dkloeck... nice explanation and great article
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question