Difference between the stack and the heap?

Discussion in 'C Programming' started by Zach, Feb 19, 2007.

  1. Zach

    Zach Guest

    Could someone please illustrate this with some ANSI C code? :)

    Zach
     
    Zach, Feb 19, 2007
    #1
    1. Advertising

  2. Zach

    Lew Pitcher Guest

    On Feb 19, 1:00 pm, "Zach" <> wrote:
    > Could someone please illustrate this with some ANSI C code? :)


    Nope.

    Primarily because the ANSI and ISO standards do not mention (let alone
    distinguish between) stack and heap.

    Sorry
    --
    Lew
     
    Lew Pitcher, Feb 19, 2007
    #2
    1. Advertising

  3. Zach wrote:
    > Could someone please illustrate this with some ANSI C code? :)
    >

    Do you mean implemented a stack and a heap, or how some runtime
    environments may utilize things called "the stack" and "the heap".

    The former can be addressed in any good book or Google search. The
    latter does not have anything to do with ANSI C, I think.
     
    Clever Monkey, Feb 19, 2007
    #3
  4. Zach

    Zach Guest

    On Feb 19, 1:18 pm, Clever Monkey
    <> wrote:
    >
    > The former can be addressed in any good book or Google search. The
    > latter does not have anything to do with ANSI C, I think.


    Ah, been reading some posts in here and see heap and stack mentioned.
    I have a vague understanding of what they are. Thought there was way
    to illustrate this with some code in C.

    Zach
     
    Zach, Feb 19, 2007
    #4
  5. Zach

    Zach Guest

    On Feb 19, 1:27 pm, "Zach" <> wrote:
    >
    > Ah, been reading some posts in here and see heap and stack mentioned.
    > I have a vague understanding of what they are. Thought there was way
    > to illustrate this with some code in C.


    To answer Clever Monkey:

    I was thinking about the latter: "the stack" and "the heap".
    Never saw or learned the former either: "stack" and "heap" in C
    yet :)

    Zach
     
    Zach, Feb 19, 2007
    #5
  6. Zach

    Rachael Guest

    On Feb 19, 6:00 pm, "Zach" <> wrote:
    > Could someone please illustrate this with some ANSI C code? :)
    >
    > Zach


    If you just declare a variable or array like this:
    int n;
    char ac[5];
    then it's on the stack. When it goes out of scope (i.e. when you exit
    the function or block in which it was declared) the memory which it
    took up is automatically given back.

    If you allocate memory using malloc, like this:
    char * pc = malloc(5);
    then the memory is allocated on the heap, and will not automatically
    be given back when the variable goes out of scope, so you have to
    explicitly free the memory:
    free(pc);

    Rachael
     
    Rachael, Feb 19, 2007
    #6
  7. Zach

    Default User Guest

    Rachael wrote:

    > On Feb 19, 6:00 pm, "Zach" <> wrote:
    > > Could someone please illustrate this with some ANSI C code? :)
    > >
    > > Zach

    >
    > If you just declare a variable or array like this:
    > int n;
    > char ac[5];
    > then it's on the stack.


    This is not required by the standard, but is often not even true when
    the system uses a stack. Automatic variables can be and sometimes are
    held in registers.

    > When it goes out of scope (i.e. when you exit
    > the function or block in which it was declared) the memory which it
    > took up is automatically given back.


    Again, no such behavior is mandated.

    > If you allocate memory using malloc, like this:
    > char * pc = malloc(5);
    > then the memory is allocated on the heap, and will not automatically
    > be given back when the variable goes out of scope, so you have to
    > explicitly free the memory:
    > free(pc);


    What you say about the lifetime is correct, but no such entity as a
    heap is required to do this.





    Brian
     
    Default User, Feb 19, 2007
    #7
  8. Zach

    Zach Guest

    Thanks for the code illustration Rachael.

    Zach
     
    Zach, Feb 19, 2007
    #8
  9. Zach

    CBFalconer Guest

    Zach wrote:
    >
    > Could someone please illustrate this with some ANSI C code? :)


    What stack? What heap? No such things are defined in ISO C.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>

    "A man who is right every time is not likely to do very much."
    -- Francis Crick, co-discover of DNA
    "There is nothing more amazing than stupidity in action."
    -- Thomas Matthews
     
    CBFalconer, Feb 19, 2007
    #9
  10. Zach

    Thad Smith Guest

    Zach wrote:
    > On Feb 19, 1:27 pm, "Zach" <> wrote:
    >
    >>Ah, been reading some posts in here and see heap and stack mentioned.
    >>I have a vague understanding of what they are. Thought there was way
    >>to illustrate this with some code in C.

    >
    > To answer Clever Monkey:
    >
    > I was thinking about the latter: "the stack" and "the heap".
    > Never saw or learned the former either: "stack" and "heap" in C
    > yet :)


    Provide enough context, usually be quoting, so that you meaning becomes
    apparent. It is not clear from the message what the latter and former are.

    --
    Thad
     
    Thad Smith, Feb 20, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,388
  2. Sean Cook
    Replies:
    15
    Views:
    48,028
    Rolf Magnus
    Jan 29, 2004
  3. Sayan
    Replies:
    6
    Views:
    7,335
    Ron Natalie
    Feb 9, 2004
  4. Jorge Rivera
    Replies:
    10
    Views:
    5,694
    John Harrison
    May 10, 2004
  5. Veeru
    Replies:
    12
    Views:
    1,325
    Default User
    Sep 14, 2006
Loading...

Share This Page