Memory Dump

Discussion in 'C Programming' started by Pacher R. Dragos, Oct 14, 2005.

  1. Can someone please tell me how do I dump memory in C.
    I tryed to define a void pointer to an offset but It doesn't work as I
    expected. A tiny example would be very nice.
     
    Pacher R. Dragos, Oct 14, 2005
    #1
    1. Advertising

  2. Pacher R. Dragos a écrit :
    > Can someone please tell me how do I dump memory in C.
    > I tryed to define a void pointer to an offset but It doesn't work as I
    > expected. A tiny example would be very nice.
    >

    Try a pointer to unsigned char.
     
    Emmanuel Delahaye, Oct 14, 2005
    #2
    1. Advertising

  3. Emmanuel Delahaye wrote:
    > Try a pointer to unsigned char.


    /* type ?? */ data = 0xffff;
    unsigned char * offset = &data;

    /* i haven't tryed this because i need a compatible type */
    printf("memory at address %p is %p ", &data, *offset);

    Any suggestions!
     
    Pacher R. Dragos, Oct 14, 2005
    #3
  4. Pacher R. Dragos a écrit :
    > Emmanuel Delahaye wrote:
    >
    >>Try a pointer to unsigned char.

    >
    > /* type ?? */ data = 0xffff;
    > unsigned char * offset = &data;


    cast required. const is a good idea too...

    unsigned char const * offset = (unsigned char const *) &data;

    > /* i haven't tryed this because i need a compatible type */
    > printf("memory at address %p is %p ", &data, *offset);


    printf ("memory at address %p is %02X\n", &data, (unsigned) *offset);

    Try that.

    #include <stdio.h>

    int main (void)
    {
    int x = 0x1234;
    unsigned char const *p = (unsigned char const *) & x;

    size_t i;

    for (i=0; i < sizeof x; i++)
    {
    printf("%02X ", p);
    }
    printf ("\n");

    return 0;
    }

    Note that the result is implementation-dependent.

    On my machine(WinTel XP/Mingw)

    34 12 00 00
     
    Emmanuel Delahaye, Oct 14, 2005
    #4
  5. void dump_memory(void* data, size_t len)
    {
    size_t i;
    printf("Data in [%p..%p): ",data,data+len);
    for (i=0;i<len;i++)
    printf("%02X ", ((unsigned char*)data) );
    printf("\n");
    }
    /* cutomize it to your heart's desire... */
     
    David Lee Lambert, Oct 14, 2005
    #5
  6. David Lee Lambert a écrit :
    > void dump_memory(void* data, size_t len)


    void dump_memory(void const* data, size_t len)
     
    Emmanuel Delahaye, Oct 14, 2005
    #6
  7. "David Lee Lambert" <> writes:
    > void dump_memory(void* data, size_t len)
    > {
    > size_t i;
    > printf("Data in [%p..%p): ",data,data+len);


    "data+len" is a constraint violation; you can't do arithmetic on void
    pointers. (gcc supports this as an extension by pretending that void*
    is char*).

    > for (i=0;i<len;i++)
    > printf("%02X ", ((unsigned char*)data) );
    > printf("\n");
    > }
    > /* cutomize it to your heart's desire... */


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 14, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Microsoft News Server

    Getting proper Stack information from memory dump

    Microsoft News Server, Nov 9, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    1,296
    Jim Cheshire [MSFT]
    Nov 11, 2004
  2. halfdog
    Replies:
    12
    Views:
    12,587
  3. Otto Barnes

    Hashtable Memory Dump?

    Otto Barnes, Aug 4, 2003, in forum: C++
    Replies:
    1
    Views:
    416
    Mike Wahler
    Aug 4, 2003
  4. Chris Cranford

    Dump Memory Pointer?

    Chris Cranford, Apr 14, 2004, in forum: C++
    Replies:
    5
    Views:
    3,831
    Christopher Benson-Manica
    Apr 15, 2004
  5. Todd
    Replies:
    4
    Views:
    566
    Jeff Higgins
    Sep 5, 2007
Loading...

Share This Page