hex values/variables location

This is a discussion on hex values/variables location within the C Programming forums, part of the General Programming Boards category; well ive been into the stack the last few days, hi! here is an example i picked up from the ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    hex values/variables location

    well ive been into the stack the last few days, hi!


    here is an example i picked up from the web:
    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv) 
    {
    
      char *somevar;
      char *important;
      char *temp;
    
    
      somevar=(char *)malloc(sizeof(char)*4);
      important=(char *)malloc(sizeof(char)*14);
    
      strcpy(important, "command");
    
      strcpy(somevar, argv[1]);
    
    
      printf("%p\n%p\n", somevar, important);
      printf("Starting To Print memory address:\n");
    
      temp = somevar;
    
      while(temp < important + 14)
      {
    
    /* this loop will be broken when we get to the last memory address we
    want, last memory address of important variable */
    
        printf("%p: %c (0x%x)\n", temp, *temp, *(unsigned int*)temp);
        temp++;
    
      }
    
      return 0;
    }
    the important function i dont understand, or cant figure out, is the printf() call.

    first off: we declared temp to be a pointer to type char.
    so what is printf doing when called that way?
    Code:
    printf("%p: %c (0x%x)\n", temp, *temp, *(unsigned int*)temp);
    i could imagine that %p is the pointer address.(the memory location in the stack?)
    %c is used with *temp (a pointer to an array of type char?), what does that do?
    and yes last but not least, there is that %x.
    does %x print the hex values of the given variable? (without preceding "0x"?)


    here are my questions listed:

    1. i could imagine that %p is the pointer address.(the memory location in the stack?)

    2.%c is used with *temp (a pointer to an array of type char?), what does that do?

    3.does %x print the hex values of the given variable? (without preceding "0x"?)


    thank you very much!!

  2. #2
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    #1. %p prints the address of the variable as you guessed

    #2. %c prints a character which is currently being pointed to by temp

    #3. Yes %x prints the hex value of the variable.

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    first off: thanks for your quick response!
    Code:
    #include <stdio.h>
    
    int main (void)
    {
    
        char *temp;
    
        printf("memory location of temp: %p\n", temp);
        return 0;
    }
    when i execute that program it shows me the memory address of the temp pointer.
    its 0x2804b5a9 for me.

    but when i run gdb for 1 and "disass main", the memory location printed before doesnt show up anywhere.
    whats the problem with that?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    temp is uninitialised, so its value is unpredictable.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    Don't forget to use free to uninitialize any memory that has been used from malloc or calloc.

  6. #6
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    thanks for the suggestion.

  7. #7
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Originally posted by Hammer
    temp is uninitialised, so its value is unpredictable.
    here my fixed code:
    Code:
    #include <stdio.h>
    
    int main (void)
    {
    
        char *temp;
        char buffer = 'A';
        temp = &buffer;
    
    
        printf("location of temp: %p\n", temp);
    
        return 0;
    }
    temp isnt anymore uninitialised.
    i get a value like: 0xbfbffbe7.
    when i run gdb on the executable again i cannot find the above value.

    whats that all about?
    thanks

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    See what results you get with this.
    Code:
    #include <stdio.h>
    
    int main (void)
    {
        char *temp;
        char buffer = 'A';
        temp = &buffer;
    
        printf("location of buffer: %p\n",   (void*)&buffer);
        printf("   value of buffer: '%c'\n",         buffer);
        printf("location of   temp: %p\n",   (void*)&temp);
        printf("   value of   temp: %p\n",   (void*) temp);
        printf("   value of  *temp: '%c'\n",        *temp);
    
        return 0;
    }
    
    /* my output
    location of buffer: 0012FF87
       value of buffer: 'A'
    location of   temp: 0012FF88
       value of   temp: 0012FF87
       value of  *temp: 'A'
    */

  9. #9
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    my problem is still persisting.

    well i am not quite sure that i use gdb the right way.
    Code:
    
    %gcc -o test test.c
    %./test
    location of buffer: 0xbfbffbdb
       value of buffer: 'A'
    location of   temp: 0xbfbffbdc
       value of   temp: 0xbfbffbdb
       value of  *temp: 'A'
    %gdb test
    GNU gdb 4.18 (FreeBSD)
    Copyright 1998 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i386-unknown-freebsd"...(no debugging symbols found)...
    (gdb) disass main
    Dump of assembler code for function main:
    0x8048494 <main>:       push   %ebp
    0x8048495 <main+1>:     mov    %esp,%ebp
    0x8048497 <main+3>:     sub    $0x18,%esp
    0x804849a <main+6>:     movb   $0x41,0xfffffffb(%ebp)
    0x804849e <main+10>:    lea    0xfffffffb(%ebp),%eax
    0x80484a1 <main+13>:    mov    %eax,0xfffffffc(%ebp)
    0x80484a4 <main+16>:    add    $0xfffffff8,%esp
    0x80484a7 <main+19>:    lea    0xfffffffb(%ebp),%eax
    0x80484aa <main+22>:    push   %eax
    0x80484ab <main+23>:    push   $0x804854b
    0x80484b0 <main+28>:    call   0x8048358 <printf>
    0x80484b5 <main+33>:    add    $0x10,%esp
    0x80484b8 <main+36>:    add    $0xfffffff8,%esp
    0x80484bb <main+39>:    movsbl 0xfffffffb(%ebp),%eax
    0x80484bf <main+43>:    push   %eax
    0x80484c0 <main+44>:    push   $0x8048563
    0x80484c5 <main+49>:    call   0x8048358 <printf>
    0x80484ca <main+54>:    add    $0x10,%esp
    0x80484cd <main+57>:    add    $0xfffffff8,%esp
    0x80484d0 <main+60>:    lea    0xfffffffc(%ebp),%eax
    0x80484d3 <main+63>:    push   %eax
    0x80484d4 <main+64>:    push   $0x804857d
    0x80484d9 <main+69>:    call   0x8048358 <printf>
    0x80484de <main+74>:    add    $0x10,%esp
    0x80484e1 <main+77>:    add    $0xfffffff8,%esp
    0x80484e4 <main+80>:    mov    0xfffffffc(%ebp),%eax
    0x80484e7 <main+83>:    push   %eax
    0x80484e8 <main+84>:    push   $0x8048595
    0x80484ed <main+89>:    call   0x8048358 <printf>
    0x80484f2 <main+94>:    add    $0x10,%esp
    0x80484f5 <main+97>:    add    $0xfffffff8,%esp
    0x80484f8 <main+100>:   mov    0xfffffffc(%ebp),%eax
    0x80484fb <main+103>:   movsbl (%eax),%edx
    0x80484fe <main+106>:   push   %edx
    0x80484ff <main+107>:   push   $0x80485ad
    0x8048504 <main+112>:   call   0x8048358 <printf>
    0x8048509 <main+117>:   add    $0x10,%esp
    0x804850c <main+120>:   xor    %eax,%eax
    0x804850e <main+122>:   jmp    0x8048510 <main+124>
    0x8048510 <main+124>:   leave
    0x8048511 <main+125>:   ret
    0x8048512 <main+126>:   mov    %esi,%esi
    End of assembler dump.
    
    am i looking in the right place?

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I'm not sure I understand your problem. Unless it is that you are looking for run-time information at compile time (like trying to find the contents of a register in the generated assembly).
    Code:
    %gcc -o test test.c
    %./test
    location of buffer: 0xbfbffbdb
       value of buffer: 'A'
    location of   temp: 0xbfbffbdc
       value of   temp: 0xbfbffbdb
       value of  *temp: 'A'
    %gdb test
    This looks correct to me. What is the problem you see?

  11. #11
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    i wanted to print the memory location of a specific variable, is it possible to find that memory location with gdb again?

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 06:37 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 01:30 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 09:02 AM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 02:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21