Difference between Stack & Heap.

Stack:

  • Value type allocates the values in Stack.
  • Function parameters are allocated on the stack
  • Local variables that are declared on the stack are not automatically initialized by the system so they usually have garbage in them until you set them
  • Variables on the stack disappear when the function exits (thus, if a function is called multiple times, it’s local variables and parameters are recreated and destroyed each time the function is called end exited).
  • Data is stored in stack using the Last In First Out (LIFO) method.
  • Stack has a limited size. Sometimes having too many local variables or large local arrays will cause stack overflow errors

Ex: Predefined data-types, Structures, Enumerator
Heap:

  • Reference types allocates the value reference in Heap.
  • Declared variables (as opposed to dynamically created ie new, malloc) are created on the heap before program execution begins, they exist the entire life of the program (although scope may prevent access to them – they still exist) and they are initialized to all zeros
  • Global variables are on the heap
  • Static local variables are on the heap (this is how they keep their value between function calls)
  • Memory allocated by new, malloc and calloc are on the heap
  • Heap has unlimited size.

Ex: Arrays, Classes, Objects, Delegates, Interfaces

Tagged . Bookmark the permalink.

4 Responses to Difference between Stack & Heap.

  1. Keith Haynes says:

    I discovered your blog web site on google and check some of your early posts. Continue to maintain up the pretty superb operate. I just additional up your RSS feed to my MSN News Reader. Looking for forward to reading more from you later on!

  2. Martin Kamerman says:

    This is the perfect blog for anyone who desires to discover about this topic. You comprehend so significantly its just about tough to argue with you (not that I actually would want?-HaHa). You surely put a new spin on a subject thats been written about for years. Good stuff, just awesome!

  3. James Cissel says:

    An impressive share, I just given this onto a colleague who was doing a little analysis on this. And he actually purchased me breakfast since I located it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I feel strongly about it and enjoy reading a lot more on this topic. If feasible, as you become expertise, would you mind updating your blog with far more details? It is extremely useful for me. Huge thumb up for this weblog post!

  4. Orevyvogy says:

    An impressive share, I just given this onto a colleague who was doing somewhat analysis on this. And he actually bought me breakfast for the reason that I found it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to talk about this, I feel strongly about it and enjoy reading far more on this topic.

Leave a Reply