Thread: HEap and stack, I'm confused

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Question HEap and stack, I'm confused

    I am reading this book and I am having a hard time understading the difference between the heap and the stack. I see some of the examples say returnign on the heap or the stack and I don't really have an idea what it means. Can someone clarify this for me.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Truth is a malleable commodity - Dick Cheney

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Some wordy definitions below. Essentially, far as C++ goes, if something is on the stack, it is freed automatically. To put things on the heap, you need to call new and ensure you delete them.

    i.e.

    string s;

    is on the stack, while

    string* s = new string();

    is on the heap.



    HEAP
    1. <programming> An area of memory used for {dynamic memory allocation} where blocks of memory are allocated and freed in an arbitrary order and the pattern of allocation and size of blocks is not known until {run time}. Typically, a program has one heap which it may use for several different purposes.

    Heap is required by languages in which functions can return arbitrary data structures or functions with {free variables} (see {closure}). In {C} functions {malloc} and {free} provide access to the heap.

    Contrast {stack}. See also {dangling pointer}.


    STACK
    <programming> (See below for synonyms) A data structure for storing items which are to be accessed in last-in first-out order.

    The operations on a stack are to create a new stack, to "push" a new item onto the top of a stack and to "pop" the top item off. Error conditions are raised by attempts to pop an empty stack or to push an item onto a stack which has no room for further items (because of its implementation).

    Most processors include support for stacks in their {instruction set architecture}s. Perhaps the most common use of stacks is to store subroutine arguments and return addresses. This is usually supported at the {machine code} level either directly by "jump to subroutine" and "return from subroutine" instructions or by {auto-increment} and auto-decrement {addressing mode}s, or both. These allow a contiguous area of memory to be set aside for use as a stack and use either a special-purpose {register} or a general purpose register, chosen by the user, as a {stack pointer}.

    The use of a stack allows subroutines to be {recursive} since each call can have its own calling context, represented by a stack frame or {activation record}. There are many other uses. The programming language {Forth} uses a data stack in place of variables when possible.

    Although a stack may be considered an {object} by users, implementations of the object and its access details differ. For example, a stack may be either ascending (top of stack is at highest address) or descending. It may also be "full" (the stack pointer points at the top of stack) or "empty" (the stack pointer points just past the top of stack, where the next element would be pushed). The full/empty terminology is used in the {Acorn Risc Machine} and possibly elsewhere.

    In a list-based or {functional language}, a stack might be implemented as a {linked list} where a new stack is an empty list, push adds a new element to the head of the list and pop splits the list into its head (the popped element) and tail (the stack in its modified form).

    At {MIT}, {pdl} used to be a more common synonym for stack, and this may still be true. {Knuth} ("The Art of Computer Programming", second edition, vol. 1, p. 236) says:

    Many people who realised the importance of stacks and queues independently have given other names to these structures: stacks have been called push-down lists, reversion storages, cellars, dumps, nesting stores, piles, last-in first-out ("LIFO") lists, and even yo-yo lists!

    [{Jargon File}]

    (1995-04-10)

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    crap I forgot I was on the general board when I was posting it till I was in class, thankx for the help.

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, 12:17 PM
  3. confused about system stack
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-05-2006, 08:14 AM
  4. Relocatable code and Symbol Table
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-10-2002, 11:05 AM
  5. Stack and Heap
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-29-2002, 09:11 AM