Thread: Stack or Heap ?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    16

    Stack or Heap ?

    Hello Guys.

    Why do we prefer huge memory allocations on heap rather then on stack (I am aware of general differences between heap and stack and how allocations are done in both - stack and queue) ? This question was asked in an interview at Google.

    Thanks in advance for your replies.

    Regards,
    Stuart.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because the heap is often the (only) area of a process that can change size at runtime. I don't see how that's related to C? As C gives no definition of the "stack" and "heap" -- instead automatic storage and dynamic storage. In most implementations the stack size is small, and fixed at compile time (or process initialisation).

    So I'd say, "chances are, given most implementations the stack will be small and fixed size, thus the memory allocation might not fit on the stack". And perhaps mention that heap allocations may be optimised for large allocations. And also releasing the memory might be difficult on the stack, since it would have to "surface to the stop" (i.e. fall out of scope) to be freed. Consider:

    Code:
    int huge[100000] = {0};
    int alsoHuge[100000] = {0};
    
    /* ... use huge and alsoHuge */
    
    Now I'm done with huge, but I (being the compiler or implementation) would have to 'pop' alsoHuge to release huge since it was probably pushed before alsoHuge.
    Of course that could be solved by an optimising compiler. Except when the dependency isn't linear...

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    And there is no easy way to check a stack overflow: with such an array, you can't call many functions since they need the stack space.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    Thanks for your reply "zacs7"

    You said that , "And perhaps mention that heap allocations may be optimised for large allocations."

    What do you exactly mean by that ?

    Thanks.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Stuart Dickson View Post
    Why do we prefer huge memory allocations on heap rather then on stack (I am aware of general differences between heap and stack and how allocations are done in both - stack and queue) ?
    That's because in most kernels the stack segment is configured smaller than the heap segment. However, that can be altered thro' the kernel parameter that's related to stack segment size. But what constricts large allocations on the stack is the upper limit on the function frames that are pushed on the stack, as the function frame size is a fraction of the stack segment size. Whereas a large allocation can fill-up the entire heap portion of the data segment, it can do so only for the function frame that is pushed on the stack.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 05:03 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM