Understanding the stack and heap

This is a discussion on Understanding the stack and heap within the C Programming forums, part of the General Programming Boards category; I always see the stack and heap mentioned in CS books. I'd like to understand more about them. Are heaps ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Understanding the stack and heap

    I always see the stack and heap mentioned in CS books. I'd like to understand more about them.

    Are heaps and stacks different areas of memory, or different ways to use memory?

    What would be a good way to learn about computer architecture?

  2. #2
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Good question!

    I'd like to know too.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    You can probably find a good article on it if you Google that explains it better than we can.

    Basically, the stack refers to where your local function variables are stored. Each nested function call makes the stack bigger by by putting its variables at the top. The stack is a preallocated large space for your program to run in.

    The heap refers to memory called allocated by malloc, calloc, or OS specific functions. The max size of the heap for your program is limited by your memory and by OS restrictions.

    Edit: Oh and heap and stack are also data structures. So when you use the plural like that or refer to a heap and a stack, then people will think you are referring to something else.
    Last edited by King Mir; 01-19-2009 at 01:02 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In a C context, stack is where the local variables, return address to previous function and other information needed to be able to call functions and return back to where you came from without "getting lost". If you call many enough functions, with large enough local variables, the stack will "run out" or "overflow" - this is normally a catastrophic failure and there is no way to recover from this.

    The heap is a "free storage", which essentially means that before you've asked for a piece of heap to be used, it is not assigned to your application, and "unused memory". In most modern OS's the heap is not assigned to the application until it requests some heap storage, and the heap is then grown according to the application's needs. It is, as King Mir explains, generally limited by the limits of the OS and the physical memory you have. In C, calling malloc, calloc or realloc will get you a piece of heap memory. Calling free (and under some circumstances realloc) will "return" the memory you got from the heap. It is good practice to ALWAYS free the heap memory.

    If you have an application that uses too much heap memory, malloc or it's friends will return a NULL pointer - which is an invalid pointer. That means "there is no heap left", and your program will not be able to get more [most likely this is permanent]. However, you still have a possibility to tell the user and do some work (as long as it doesn't require you to allocate more memory) to clean up and exit. This is less bad than the running out of stack, but it's still best avoided...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    "free storage" or "heap"

    <matsp> right.

    and also :

    Dear Mr Stroustrup,

    Sorry to disturb you again. You have mentioned several times in the TC++PL that 'new' allocates memory from the 'free store (or heap)'. There has been a huge cry on the C++ community at Orkut (that I am moderating) as to whether free-store is the same as heap. The argument given against is that Mr Herb Sutter has mentioned that the free-store is different from the heap:

    http://www.gotw.ca/gotw/009.htm

    and that global 'new' has nothing to do with the heap.

    So, if so, why has TC++PL used 'free store (or heap)' instead of mentioning the use of 'heap' separately.

    Waiting anxiously for the response.

    Regards,
    Zaman Bakshi

    His Reply:


    Note that Herb says: "Note about Heap vs. Free Store: We distinguish between "heap" and "free store" because the draft deliberately leaves unspecified the question of whether these two areas are related. For example, when memory is deallocated via operator delete, 18.4.1.1 states:"

    In other word, the "free store" vs "heap" distinction is Herb's attempt to distinguish malloc() allocation from new allocation.

    >
    > So, if so, why has TC++PL used 'free store (or heap)' instead of
    > mentioning the use of 'heap' separately.

    Because even though it is undefined from where new and malloc() get their memory, they typically get them from exactly the same place. It is common for new and malloc() to allocate and free storage from the same part of the computer's memory. In that case, "free store" and "heap" are synonyms. I consistently use "free store" and "heap" is not a defined term in the C++ standard (outside the heap standard library algorithms, which are unrelated to new and malloc()). In relation to new, "heap" is simply a word someone uses (typically as a synonym to "free store") - usually because they come from a different language background.


    from : http://zamanbakshifirst.blogspot.com...rsus-heap.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and heap objects
    By John_L in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 10:20 AM
  2. stack vs heap memory allocation
    By gongchengshi in forum C Programming
    Replies: 9
    Last Post: 11-18-2007, 11:17 AM
  3. Relocatable code and Symbol Table
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-10-2002, 11:05 AM
  4. What is a Stack, Heap and Queue ?
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 03-17-2002, 11:16 AM
  5. Stack and Heap
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-29-2002, 08:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21