Closed Thread
Results 1 to 3 of 3

Thread: Trying to learn more about stack/heap/global data

  1. #1
    blernblan is offline Newbie
    Join Date
    Apr 2009
    Posts
    9
    Rep Power
    0

    Question Trying to learn more about stack/heap/global data

    Hey guys, I'm trying to learn more about exactly what C stores on the stack, what it stores on the heap and what is considered global data etc. So any help is greatly appreciated. For example:

    Code:
    int x = -2;
    This is a global variable (not inside a function). Would this be stored on the code/global data memory image section? Or would this go on the stack?

    Another question:

    Code:
    int f(int b)
    {
        return b - x;
    }
    Is the variable b on the stack even though it's a formal parameter and not an actual? Or would it not be pushed onto the stack until used in the return statement of return b - x;?

    Thanks for any help guys!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Trying to learn more about stack/heap/global data

    The heap is used to store memory allocated using malloc(). It is accessed using pointers.
    The stack stores pretty much everything else.
    Global variables exist from the time the program starts until it ends, unlike other variables that are created/destroyed on the stack as they enter/leave scope.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Trying to learn more about stack/heap/global data

    That's true, but you didn't mention data segments. Static data (string constants,etc.) and global variables are stored in data segments that are separate from the stack and code segments. All local variables are stored on the stack. Note that when things are allocated with new or malloc, the pointer to the object is local, but the physical bytes comprising that object are stored on the heap.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C++ How to set stack/heap size?
    By pancakethirsty in forum C and C++
    Replies: 10
    Last Post: 07-12-2011, 10:30 PM
  2. Replies: 3
    Last Post: 11-26-2010, 08:53 AM
  3. General What is .sect, .data, .text, and .global?
    By MDM in forum Assembly
    Replies: 7
    Last Post: 03-05-2010, 10:25 PM
  4. Strings in Stack and Heap
    By ibad in forum Java Help
    Replies: 1
    Last Post: 08-27-2009, 01:03 PM
  5. Global Variables vs Data Duplicates
    By scc in forum General Programming
    Replies: 4
    Last Post: 07-28-2008, 06:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

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