printing memory at address..help!

This is a discussion on printing memory at address..help! within the C Programming forums, part of the General Programming Boards category; I was curious as to how if a user inputsa string: such as 08984732 (a hexadecimal form), and I convert ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    13

    printing memory at address..help!

    I was curious as to how if a user inputsa string: such as 08984732 (a hexadecimal form), and I convert it to an int, how can I print out the contents of the address with that number...

    it's kinda weird, I don't want to print the address of the int.. i want to print the contents of the address who's address is the int that is inputted.. can someone help?

    john

  2. #2
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >how can I print out the contents of the address with that number...
    A better question would be, how do you know that the address is valid? But you could do it something like this:
    Code:
    #include <stdio.h>
    
    int main ( ) {
      int i = 10;
      int j = 0x0012FF78;        /* Address of i on my box */
      int *pi = (int*)j;
    
      printf("%p\n", (void*)&i); /* Prints 0x0012FF78 */
      printf("%p\n", (void*)pi); /* Prints 0x0012FF78 */
      printf("%d\n", *pi);       /* Prints 10 */
      
      return 0;
    }
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    13
    hmm okay thanks that works,, but now i have a new problem.. the user is supposed to input a 32 bit integer.. or a 8 hex digit l number (as a string first).. i am using the following to convert it to an integer..

    int dec;
    dec = (int) strtol(address, NULL, 16);

    that works for any number inputted up to 28 bits (7 digits of hex) but on the 8th one it keeps spitting back 7fffffff when i test to see if it converted correctly... address is a string orginally.

    so after doing that i tried doing what you did except with dec instead of j..

    int *p = (int *)dec;

    then i tried printing that.. except it gives a bus error..

  4. #4
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >except it gives a bus error..
    I'm shocked...no, not really. Like I said in my first post, how do you know that the address you're given is valid and accessable by your program? Just picking numbers out of the blue will give you a valid address enough times to count as dumb luck. And if the address you're trying to access is something that doesn't change (like video memory that you can write to), why bother entering it as a string to begin with? You can just as easily hard code the address.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    13
    hmm i got it to work... just i used sscanf to load the number, the other function was being dumb...

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>the other function was being dumb...
    Yeah, standard functions do that a lot. Oh no, wait a minute, that's users.

    Maybe if you're having trouble you should post some code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Asking windows for memory on a fixed address
    By pheres in forum Windows Programming
    Replies: 4
    Last Post: 03-23-2009, 11:15 AM
  2. Pointer to specific memory address
    By elnerdo in forum C++ Programming
    Replies: 10
    Last Post: 05-19-2006, 07:35 AM
  3. how to get memory address applied in other program?
    By wu7up in forum C Programming
    Replies: 1
    Last Post: 03-01-2003, 12:44 AM
  4. Memory address?
    By Munkey01 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 08:04 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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