heap and stack ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geiregat Jonas

    #1

    heap and stack ?

    I'm reading Eric Gunnerson's book.
    He is talking about the heap and stack, he says you have 2types, value
    wich are in the stack or inline or reference types wich are in the heap.

    I don't get this what's heap stack and what's the main difference
    between those 2types ?

  • Eric Gunnerson [MS]

    #2
    Re: heap and stack ?

    The heap and the stack are the two places where programs store data (okay,
    there are a few more, but they're the two most common ones).

    The heap is just a big chunk of memory that is managed by a heap manager (or
    garbage collector, in C#'s case). When you write something like:

    Employee e = new Employee();

    (assuming Employee is a class), you get a chunk of memory allocated out of
    the heap to store the information for that employee instance. This instance
    will be preserved until it isn't needed anymore (in C#), or until it is
    explicitly deleted (in languages like C++).

    The stack is used to hold local variables. So, when I write something like:

    int i = 15;

    as part of a method, that sets aside a chunk of space on the stack to hold
    the value "15". When I enter a method, that space is used on the stack, and
    when I leave the method, that space is reclaimed for later use.

    To be strictly true, in my first example, I not only allocated an instance
    of Employee on the heap, I allocated a reference to that on the stack.

    Or, to be shorter, all local variables use up space on the stack. If they
    are value types, this space is used to store the actual value. If they are
    references types, this space is used as a reference (aka pointer) to the
    actual value, which is stored on the heap.

    Hope that helps.

    --
    Eric Gunnerson

    Visit the C# product team at http://www.csharp.net
    Eric's blog is at http://blogs.gotdotnet.com/ericgu/

    This posting is provided "AS IS" with no warranties, and confers no rights.
    "Geiregat Jonas" <eniac@sdf-eu.org> wrote in message
    news:3fccf20b$0 $3239$ba620e4c@ reader0.news.sk ynet.be...[color=blue]
    > I'm reading Eric Gunnerson's book.
    > He is talking about the heap and stack, he says you have 2types, value
    > wich are in the stack or inline or reference types wich are in the heap.
    >
    > I don't get this what's heap stack and what's the main difference
    > between those 2types ?
    >[/color]


    Comment

    Working...