stack, heap, confusion
Results 1 to 4 of 4

Thread: stack, heap, confusion

  1. #1
    Senior Member
    Join Date
    Apr 2002
    Posts
    161

    Unhappy stack, heap, confusion

    Hi everyone, I've got a few OS/memory questions:
    When you write a program, lets say in C, how is memory allocated for variables, functions, callocs/mallocs?
    What are the main differences between the stack and the heap?
    Also, when you have multiple threads, all have access to the same memory, is this accomplished by just sharing pointers?

    I have searched around for a concise answer, but AO is always a better resource for no BS info.

    thanks,
    J

  2. #2
    Senior Member
    Join Date
    Jun 2003
    Posts
    772
    Multiple threads have a seperated stack.
    Variables are normally put in the stack.
    Well memory sort off looks like this:
    (Note that this is not always so, there are systems were the stack grows up, this is most common though (x86))
    -----------------------
    |
    Auxilary regions
    |
    -----------------------

    -----------------------
    Stack(grows down)
    -----------------------


    ------------------------
    Heap(grows up)
    ------------------------
    ------------------------
    Data (contains global and static vars)
    -------------------------
    ---------------------------------------
    Text (program code here)
    ---------------------------------------


    Operations on the stack always happen at the top, stuff gets pushed on the stack and stuff gets popped of the stack. (LIFO strucure). The stack stores variables that are local to functions.
    The heap is dynamic, the programmer controls the creation/deleting of objects. There can also be memory holes (it is not organised like the stack).
    The heap is actually simply your computer's unused memory.

    malloc() etc. allocate memory on the heap.
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

  3. #3
    Senior Member
    Join Date
    Dec 2003
    Location
    Pacific Northwest
    Posts
    1,675
    Great job El-half, doggon it, you bet me to it. Well I'll post it anyway.

    Hummmm, seems you want me to do your homework for your class…lol. Ok, I’ll byte.

    So here we go: The C program places information in memory by using local and global variables. And of course we cannot forget it uses structures and arrays as well. The problem with this is that you must establish/fix the amount during compile. Then what happens if you didn’t allocate enough? Fortunately folks a lot smarter than me cured the problem.

    C also uses a dynamic-allocation memory system. That is a process in which during runtime of the program, memory is allocated (until it is all spent). The process is most beneficial during the use of applications. Some applications may require the full use of all the memory in your computer. There are some obvious benefits (i.e. A user might need to edit extremely large documents.) or ”..you have multiple threads, all needing access to the same memory,…” The dynamic-allocation system is also used for binary trees and linking lists.

    Malloc( ) is a function and is the heart of the allocation process. It returns the pointers to allocated pieces of memory. Example:

    {
    struct addr *p;

    if ( ( p = malloc (size (struct addee) ) ) = = Null)
    }
    return p;
    }


    Calloc( ) is a function that will return a pointer to the very first byte of the allocated area.

    Example:
    {
    float *p;

    p = calloc (200, size(float));
    if……;
    }
    return p;
    /* returns 200 floats */

    ***Both malloc( ) and calloc( ) require the use of #include (stdlib.h)***

    Free( ) is also a critical part of the system. If malloc( ) returns a null pointer you will need to free memory. This is accomplished by calling free( ) with a pointer to the start of the memory previously allocated by malloc( ). Calloc( ) will return a null pointer as well if there is insufficient memory.

    Heap: the memory location (free) in which the storage of information is allocated. Lies between the program and the stack.

    Stack: (for your question) maintains the returns (return addees) of function calls, the arguments and local variables

    Variable (char, int, float, double, void, etc) are named memory locations.

    Additionally I was surprised you didn’t include the other two regions of memory. The first actually holds the programs exe code and the remaining is where the global variables is stored. Wasn’t on the sheet…lol? Well it looks like this: Stack > Heap > Global variables > Program code.

    Good luck and thanks for letting me do some reviewing as well!

  4. #4
    Senior Member
    Join Date
    Apr 2002
    Posts
    161
    Cool, thanks a lot, very complete and concise.

    peace,

    J

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •