Thread: View memory?

  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    View memory?

    Is there a simple way to view the memory? like so?

    Code:
    0012FF64:  36  E8  76  3B  B8  FF  12  00  26  24  41  00  01  00  00  00      6.v;..↕.&$A.....

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you have access to it, yes. Good luck with that under XP or any protected mode operating system.

    You can look at memory you have allocated or have been given access to. Since poking around in another processes memory is really not how well-behaved programs work, XP for one, won't grant you access to any memory location you want.

  3. #3
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by Bubba View Post
    If you have access to it, yes. Good luck with that under XP or any protected mode operating system.

    You can look at memory you have allocated or have been given access to. Since poking around in another processes memory is really not how well-behaved programs work, XP for one, won't grant you access to any memory location you want.
    say i am writing a program, and store something of cahr type... and then i want view it... that isn't possible? i think i have stumbled across it before... using a pointer?

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Yes. If you allocated the memory, the OS will let you access it. That's why you allocate it in the first place.

    Code:
    mytype * ptr;      //ptr is some sort of integer that holds an address in memory.
    ptr = new mytype;                 //"new mytype" is a function that asks the OS for some memory
                                  // to hold a "mytype", sets up the memory and then returns the address.
                                  //So now ptr contains that address.
    std::cout << ptr << std::endl;   //will display the address that we just spoke about
    std::cout << *ptr << std::endl; //will go to that address and tell cout to display the object there
                                      //(Hopefully you told cout previously what to do with it)
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    ok so i tried to convert it to using "printf()"
    Code:
    printf("%p",*ptr);
    But it only displays the memory address? should i be using something other than "%p" ?

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Code:
    printf("&#37;p",*ptr);
    %p means "print a pointer."
    *ptr is not a pointer. *ptr is (hopefully) some kind of object (which could be a pointer, but I doubt that's what you meant).

    Think of it this way:
    Remember that every pointer is an address.
    " * " means "value at address"
    " & " means "the address of"


    So.... if you want to print the number 67... How could we finish this code so it does?
    Code:
    int* a;
    int b = 67;
    
    a = /*What should I set it to?*/;
    
    std::cout << *a << std::endl;
    Last edited by CodeMonkey; 08-01-2007 at 12:01 AM. Reason: more
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    ok, i get that... But even when i change it to

    Code:
    printf("%p",&ptr);
    i still only get the address of the pointer, is it possible get this
    Code:
    0012FF64:  36  E8  76  3B  B8  FF  12  00  26  24  41  00  01  00  00  00      6.v;..↕.&$A.....
    using printf()? or dose it have to be done using cout?

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    You can do it however you like. The question is: Do you have access to that memory?

    Code:
    const int size = 16;
    char data[size];
    
    //let's see what that looks like -- not having initialized the data
    printf("&#37;X: ", &data[0]);
    
    for(int x = 0; x < size; ++x) printf("%X ",data[x]);
    
    printf("    ");
    
    //now I would treat data as a string, but we don't know where
    //the null character is!
    
    for(int y = 0; y < size; ++y) printf("%c",data[y]);
    
    //and that should do it

    *edit* Interesting. When I ran that on my computer, some of the chars were displayed as much bigger than a char. How does printf() know that the hexadecimal arguments are chars?
    Last edited by CodeMonkey; 08-01-2007 at 12:19 AM. Reason: more
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >How does printf() know that the hexadecimal arguments are chars?
    Cast to unsigned char first, which is promoted to unsigned int.
    Code:
    for(int x = 0; x < size; ++x) printf("%X ", (unsigned char) data[x]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM