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

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    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. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by blernblan View Post
    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?

    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;?
    I am probably not to be trusted with this stuff but I would say in the first case, it's on the heap, and in the second *definitely* in the stack.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by blernblan View Post
    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?
    Variable x would be stored in the data segment (initialized).
    Quote Originally Posted by blernblan View Post
    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!
    Parameter variable b would be on the stack and when f() returns its entire frame is popped from the stack.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM