Stack Memory Managment

Discussion in 'C' started by jayaraj_ev, Mar 8, 2009.

  1. jayaraj_ev

    jayaraj_ev New Member

    Joined:
    Aug 29, 2007
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software engineer
    Location:
    Bangalore

    Memory Segment



    Whenever U create a program and load it on a CPU ie executing UR program. It loads a Process Page on the virtual memory of UR system.That Process page will be mapped to UR Task Struct which will be in UR Process Control Block PCB.

    [​IMG]

    Ps: Stack Segment and Heap Segment may grow other way too (UP and Down respectively)as it depend on the system U work
    • Intel/Windows Stack grows Upwards
    • Sparc/Solaris Stack grows Downwards

    When a program is executed a Function Stack Frame is created in the Stack segment of UR Process Page for each and every function called and destroyed when U exit from the function scope.

    Function Stack Frame



    [​IMG]

    All Task structs have two Pointer called the Stack Pointer (SP), and the Frame (FP) or Base Pointer (BP). SP always points to the "top" of the stack, and FP always points to the "top" of the frame.

    So when function are called inside functions the stack keeps growing downward. when Functions are called:
    1. Saves local variables and temporaries
    2. The fn called pushes the parameters of the fn to be called into the stack
    3. The Program counter is saved as “return address”
    4. SP pointing to previous stack is copied to the new FP

    In a small program like :
    Code:
    void fn (void)
    {
        int j;
        return;
    }
    int main (void)
    {
         int i; 
        fn();
       return 0;
    }
    The Stack frame for this program would be:
    [​IMG]

    To test these we can write a small Program:

    Code:
    #include <iostream>
    using namespace std;
    
    void fn(void)
    {
        int * ptr;
        ptr = (int *)&ptr;
        ++ptr;
        int* mainfp = (int*)(*ptr);
        --mainfp;
        --mainfp;
         *mainfp = 20;
        return;
      }
    
    int main (void)
    {
       int i = 10;
       fn();
       cout<<”I =<<i<<endl;
       return 0;
    }
    Ps : This program works with Ubuntu linux. Try it for windows or sun sparc.
     
    Last edited by a moderator: Jan 21, 2017
  2. jayaraj_ev

    jayaraj_ev New Member

    Joined:
    Aug 29, 2007
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software engineer
    Location:
    Bangalore
    For those using Intel/Windows m/c

    Try this

    void fn(void)
    {
    int *ptr;
    ptr = (int *)&ptr;
    ++ptr;
    ++ptr;
    int* mnptr = (int *) *ptr;
    --mnptr;
    --mnptr;
    *ptr = 300;
    return;
    }
     
  3. jayaraj_ev

    jayaraj_ev New Member

    Joined:
    Aug 29, 2007
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software engineer
    Location:
    Bangalore
    Hi Guys ,
    Sorry that i missed one Infor about the stack frame.

    The stack frame would be like
    ____________________________
    |_______Local Variables_______ |
    |________Padding____________ |
    |______Frame Pointer_________ |
    |______Return Address________ |
    |_Arguments/saved Temporaries_|

    So to calculate padding = if ((Total size of local variables)% or mod 8) then padding = 4 else padding = 0;

    so when You need to calculate Total size of a Stack frame : Size of all parameter + sizeof RA+ sizeofFP + Padding + sizeof Local

    In the example

    Code:
    void fn(void)
    {
    int *ptr;
    ptr = (int *)&ptr;
    ++ptr;    //Points to padding memory.
    ++ptr;   //points to FP where is stores previous FP
    int* mnptr = (int *) *ptr;
    
    --mnptr;  //points to Padding memof Main fn
    --mnptr;  //points to Local variable.
    *ptr = 300;
    return;
    } 
    
    int main(void)
    {
      int i = 0;
      fn();
      cout<<"I ="<<i<<endl;
    
      return 0;
    }
     
  4. TriG0rZ

    TriG0rZ New Member

    Joined:
    Oct 2, 2008
    Messages:
    88
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    Wow, this is really good and i needed to know this lol for uni

    thanks <3
     
  5. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    Good article..Jayaraj !!! Keep posting excellent articles...
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Article of the month competition nomination started here
     
  7. MHpurple

    MHpurple New Member

    Joined:
    Apr 6, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I am searching for this only thnks for the post very use ful
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice