Memory Dump

P

Pacher R. Dragos

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.
 
E

Emmanuel Delahaye

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.
 
P

Pacher R. Dragos

Emmanuel said:
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!
 
E

Emmanuel Delahaye

Pacher R. Dragos a écrit :
/* 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
 
D

David Lee Lambert

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... */
 
K

Keith Thompson

David Lee Lambert said:
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... */
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top