Thread: variable memory location with ampersand vs pmap

  1. #1
    Registered User setevoy's Avatar
    Join Date
    Jul 2014
    Location
    Kiev, UA
    Posts
    4

    variable memory location with ampersand vs pmap

    I just started deal with pointers in C, and one stuff confusing me...
    Here is simple code:
    Code:
    int main ()
    {
    
       int var1 = 10;
       int var2 = 20;
    
       long ms = 10000000;
    
       printf("Address of var1 variable: %p\n", &var1  );
       printf("Address of var2 variable: %p\n", &var2  );
    
       retpid();
       millisleep(ms);
    
       return 0;
    }
    And - it's return var1 and var2 memory address (virtual memory, I believe?):
    Code:
    $ ./address 
    Address of var1 variable: 0x7fff0d7f7984
    Address of var2 variable: 0x7fff0d7f7980
    PID = 16496
    But - when I run pmap - I don't see this addresses there:

    Code:
    # pmap -x 16496
    16496:   ./address
    Address           Kbytes     RSS   Dirty Mode   Mapping
    0000000000400000       4       4       0 r-x--  address
    0000000000600000       4       4       4 rw---  address
    00007f79cf8b8000    1576     260       0 r-x--  libc-2.12.so
    00007f79cfa42000    2048       0       0 -----  libc-2.12.so
    00007f79cfc42000      16      16      16 r----  libc-2.12.so
    00007f79cfc46000       4       4       4 rw---  libc-2.12.so
    00007f79cfc47000      20      12      12 rw---    [ anon ]
    00007f79cfc4c000     128     104       0 r-x--  ld-2.12.so
    00007f79cfe5c000      12      12      12 rw---    [ anon ]
    00007f79cfe69000       8       8       8 rw---    [ anon ]
    00007f79cfe6b000       4       4       4 r----  ld-2.12.so
    00007f79cfe6c000       4       4       4 rw---  ld-2.12.so
    00007f79cfe6d000       4       4       4 rw---    [ anon ]
    00007fff0d7e5000      84      12      12 rw---    [ stack ]
    00007fff0d7ff000       4       4       0 r-x--    [ anon ]
    ffffffffff600000       4       0       0 r-x--    [ anon ]
    ----------------  ------  ------  ------
    total kB            3924     452      80

    I guess - it's is inside stack:

    Code:
    # cat /proc/16496/maps | grep stack
    7fff0d7e5000-7fff0d7fa000 rw-p 00000000 00:00 0                          [stack]

    But - how I can obtain this variables addreses, to see them with `pmap`? If I put them as "global":

    Code:
    ...
    #include <stdlib.h>
    #include <sys/types.h>
    
    int var1 = 10;
    int var2 = 20;
    
    int millisleep(unsigned ms);
    int retpid(void);
    
    int main ()
    {
    
    //   int var1 = 10;
    //   int var2 = 20;
    
       long ms = 10000000;
    
       printf("Address of var1 variable: %p\n", &var1  );
       printf("Address of var2 variable: %p\n", &var2  );
    
       retpid();
       millisleep(ms);
    
       return 0;
    }
    ...
    I have odd addresses in result:

    Code:
    Address of var1 variable: 0x600a94
    Address of var2 variable: 0x600a98
    Thanks.
    Last edited by setevoy; 10-23-2014 at 01:32 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So what's the problem?

    7fff0d7e5000 + 84 * 1024 == 7fff0d7fa000

    And these lie between those two addresses

    Address of var1 variable: 0x7fff0d7f7984
    Address of var2 variable: 0x7fff0d7f7980


    Similarly,
    Address of var1 variable: 0x600a94
    Address of var2 variable: 0x600a98

    lie between
    600000 4 * 1024 == 601000
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory location of variable
    By justine in forum C Programming
    Replies: 9
    Last Post: 11-27-2012, 05:01 PM
  2. Replies: 0
    Last Post: 05-07-2009, 01:31 AM
  3. allocate apecified memory location for a c variable
    By BharathKumar in forum Linux Programming
    Replies: 5
    Last Post: 06-01-2007, 03:47 PM
  4. Memory Location
    By SomC in forum C Programming
    Replies: 6
    Last Post: 05-08-2004, 02:54 AM
  5. memory location
    By xlordt in forum C Programming
    Replies: 3
    Last Post: 10-21-2002, 09:39 AM