Thread: Is there a logical way of figuring out where variables are stored?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    47

    Is there a logical way of figuring out where variables are stored?

    Hi guys.

    I'm trying to figure out the following for each variable In a program:

    1. the runtime stack, including the stack base and the direction of stack growth;
    2. the dynamic data area (the heap);
    3. the static data area, including the initialized and uninitialized data segments (objects with fixed addresses);
    4. the text area (the program instructions).


    based on the address I get for a UNIX system.

    I realize for every different OS it will have a different addressing scheme.

    Here is an example, from the following program:
    Code:
    #include <stdio.h>      /* for printf() */
    #include <stdlib.h>     /* for malloc(), putenv(), setenv() */
    #include <string.h>     /* for strlen() */
    #include <math.h>       /* for sqrt() */
    
    #include "pr4.h"        /* for macros show...() */
    
    /*----------------------------------------------------------------------*/
    
    /* These should be at various locations in the address space. */
    
    int global_var_1, global_var_2;
    
    // etc.
    
    /*----------------------------------------------------------------------*/
    
    int main(int argc, char *argv[], char *envp[])
    {
      /* local and global variables */
    
      int local_main_var_1, local_main_var_2;
    
      // etc.
    
      show_int(global_var_1);
      show_int(global_var_2);
    
      show_int(local_main_var_1);
      show_int(local_main_var_2);
    
      // etc.
    
      return 0;
    }
    It will output the following:
    Code:
    
    demo.sun.32.sorted
    00000000ffbffa70 ( 4)         local_main_var_1          0
    00000000ffbffa6c ( 4)         local_main_var_2          -4195516
    0000000000020f84 ( 4)             global_var_2          0
    0000000000020f80 ( 4)             global_var_1          0
    
    
    demo.sun.64.sorted
    ffffffff7ffff930 ( 4)         local_main_var_1          0
    ffffffff7ffff92c ( 4)         local_main_var_2          2147482328
    0000000100100e68 ( 4)             global_var_2          0
    0000000100100e64 ( 4)             global_var_1          0

    Now this is the output from a 64 bit UNIX system and a 32 bit unix system.

    As you can see, the local variables that are stored on the stack, both start with:
    ffffffff7ffff on the 64 bit system
    and on the 32 bit system all the local variables start with a:
    ffbffa

    I also ran this on a different computer (32 bit) and got the following for local variables:
    Code:
    00000000ffbff9c0 ( 4)         local_main_var_1          0
    00000000ffbff9bc ( 4)         local_main_var_2          -4195680
    So you can see the local variables are starting with ff

    And all the global varaibles are starting with numbers, such as 10 or 20

    Can I simply match the address to a certain pattern, like if the address starts with a letter "f" then mark that the address is in the stack area, if the address starts with a number (0 through 9) then mark that varaible that it must be in the static area (for global).

    It is possible to figure out, I'm just not sure where to even start with this...here is basically what I'm trying to achieve:

    Code:
       
       Runtime stack, bottom (stack grows downward)
       ffffffff7ffff930 ( 4)         local_main_var_1          0
        ffffffff7ffff92c ( 4)         local_main_var_2          2147482328
        Runtime stack, top
     
          Static data area
          0000000100100e68 ( 4)             global_var_2          0
          0000000100100e64 ( 4)             global_var_1          0
    You can see, its figuring out that global variables, are in the static area, and that the local variables are in the runtime stack, and that also the stack is growing downwards.

    So just from the given information what would you recommend I analyze?
    Again this is only for a Solaris Unix system, nothing else matters.

    Thanks any help would be great.
    Last edited by mr_coffee; 09-28-2008 at 09:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  3. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 06:42 PM
  4. Classes, dynamic variables, and leaks.
    By Grins2Pain in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2003, 03:07 PM
  5. static variables
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2003, 07:13 PM