Heap and Stack

Discussion in 'C Programming' started by dust, Aug 25, 2010.

  1. dust

    dust Guest

    Hi,

    I found a article ( http://www.maxi-pedia.com/what is heap and stack )
    about heap and stack.
    In this article (in sub-section "Heap and stack from programming
    perspective") I see that storage for local variables for function
    called by main (calcSize() in this ex.) is allocated in heap. Is it
    true?. What I was assuming is space for local variables of function
    called by main will be allocate in separate stack frame (within main
    stack) separated by called "Activation record".
    Could you please explain me this.

    --
    Dust
    dust, Aug 25, 2010
    #1
    1. Advertising

  2. dust

    Mark Bluemel Guest

    dust wrote:
    > Hi,
    >
    > I found a article ( http://www.maxi-pedia.com/what is heap and stack )
    > about heap and stack.


    Well done. Have a gold star.

    > In this article (in sub-section "Heap and stack from programming
    > perspective") I see that storage for local variables for function
    > called by main (calcSize() in this ex.) is allocated in heap. Is it
    > true?.


    Possibly in some instance(s)/implementation(s), perhaps not in others.
    The C language has no view on the matter

    > What I was assuming is space for local variables of function
    > called by main will be allocate in separate stack frame (within main
    > stack) separated by called "Activation record".


    Possibly in some instance(s)/implementation(s), perhaps not in others.
    It's not something that the language cares about, so it's not something
    that the language specifies
    Mark Bluemel, Aug 25, 2010
    #2
    1. Advertising

  3. dust

    osmium Guest

    "dust" wrote:

    > I found a article ( http://www.maxi-pedia.com/what is heap and stack )
    > about heap and stack.
    > In this article (in sub-section "Heap and stack from programming
    > perspective") I see that storage for local variables for function
    > called by main (calcSize() in this ex.) is allocated in heap. Is it
    > true?. What I was assuming is space for local variables of function
    > called by main will be allocate in separate stack frame (within main
    > stack) separated by called "Activation record".
    > Could you please explain me this.


    I scanned that article and do not like it one little bit. There must be
    something better around, somewhere on the web.
    osmium, Aug 25, 2010
    #3
  4. dust

    Tom St Denis Guest

    On Aug 25, 9:00 am, "osmium" <> wrote:
    > "dust" wrote:
    > > I found a article (http://www.maxi-pedia.com/what is heap and stack)
    > > about heap and stack.
    > > In this article (in sub-section "Heap and stack from programming
    > > perspective") I see that storage for local variables for function
    > > called by main (calcSize() in this ex.) is allocated in heap. Is it
    > > true?.  What I was assuming is space for local variables of function
    > > called by main will be allocate in separate stack frame (within main
    > > stack) separated by called "Activation record".
    > > Could you please explain me this.

    >
    > I scanned that article and do not like it one little bit.   There must be
    > something better around, somewhere on the web.


    It's not only a garbage presentation but it's also incorrect. Global
    scope variables are never [so far as I've seen] placed on any form of
    "stack". They're usually in a form of .bss segment if they're not
    initialized.

    I'm not going to bother reading the rest....

    Tom
    Tom St Denis, Aug 25, 2010
    #4
  5. On Aug 25, 3:03 pm, dust <> wrote:
    >
    > In this article (in sub-section "Heap and stack from programming
    > perspective") I see that storage for local variables for function
    > called by main (calcSize() in this ex.) is allocated in heap. Is it
    > true?.
    >

    Normally an implementation has a stack. Each time a function is called
    its parameters and local variables are pused on the stack. When the
    function returns the stack is popped and the memory is reused.

    However large variables, and variables whose size is not known at
    compile time, are allocated from the heap, with a call to malloc().
    Only a pointer to the memory area is placed on the stack. This is
    because most systems work better with relatively small stacks (it's
    easier to keep the stack in the cache), and because it creates
    difficulties if objects are resized when on the stack. calcSize()
    could well call malloc(). It's very common to do this.



     
    Malcolm McLean, Aug 25, 2010
    #5
  6. dust

    Seebs Guest

    On 2010-08-25, dust <> wrote:
    > I found a article ( http://www.maxi-pedia.com/what is heap and stack )
    > about heap and stack.


    It is wrong.

    > In this article (in sub-section "Heap and stack from programming
    > perspective") I see that storage for local variables for function
    > called by main (calcSize() in this ex.) is allocated in heap. Is it
    > true?.


    Okay, there's two answers here.

    One is: The concepts "heap" and "stack" are not necessarily part of how
    a C implementation works. As long as everything works, the compiler is
    free to store anything anywhere it wants.

    The second is: On many common implementations, function calls use a
    region of memory called the "stack" to store arguments and local variables,
    while malloced memory is stored in another region called the "heap". This
    is not universal, and there can be major surprises in it. The most
    consistent, and significant, exception is that a lot of values are stored
    in registers, and on many implementations, values in registers can be
    pushed out to the stack many function calls away from where they were
    local variables.

    > What I was assuming is space for local variables of function
    > called by main will be allocate in separate stack frame (within main
    > stack) separated by called "Activation record".


    That's certainly possible, although it's not part of the formal specification.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Aug 25, 2010
    #6
    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. joe

    stack and heap question

    joe, Aug 4, 2004, in forum: Java
    Replies:
    3
    Views:
    424
    Michael Borgwardt
    Aug 5, 2004
  2. Steve Hill

    vector.swap and heap / stack

    Steve Hill, Aug 20, 2003, in forum: C++
    Replies:
    5
    Views:
    988
    tom_usenet
    Aug 22, 2003
  3. Michal Slocinski

    Heap dump file size vs heap size

    Michal Slocinski, Mar 25, 2008, in forum: Java
    Replies:
    1
    Views:
    722
    GArlington
    Mar 25, 2008
  4. viki
    Replies:
    6
    Views:
    556
    Erik Wikström
    Jun 28, 2008
  5. Raymond Schanks
    Replies:
    0
    Views:
    504
    Raymond Schanks
    Apr 11, 2010
Loading...

Share This Page