Dump Memory Pointer?

C

Chris Cranford

Is there anyway that when a function is passed a memory pointer that the
contents of that pointer can be dereferenced or the bytes dumped to a
file or console?

The problem is i have no idea what the pointer really is to and nor do I
have any idea what the total size of the pointer is. I'm trying to
debug something for a friend and this has me stumped and I don't know if
it's even possible.

TIA,
cc
 
D

David Harmon

On Wed, 14 Apr 2004 15:03:59 -0400 in comp.lang.c++,
(e-mail address removed) (Chris Cranford) wrote,
Is there anyway that when a function is passed a memory pointer that the
contents of that pointer can be dereferenced or the bytes dumped to a
file or console?

The problem is i have no idea what the pointer really is to and nor do I
have any idea what the total size of the pointer is. I'm trying to
debug something for a friend and this has me stumped and I don't know if
it's even possible.

I suppose you have to guess as to the size of memory area you want to
look at.

Cast the pointer to (unsigned char *). Then hit it with something like
an adaptation of
http://groups.google.com/[email protected]
 
I

Ioannis Vranos

Chris Cranford said:
Is there anyway that when a function is passed a memory pointer that the
contents of that pointer can be dereferenced or the bytes dumped to a
file or console?

The problem is i have no idea what the pointer really is to and nor do I
have any idea what the total size of the pointer is. I'm trying to
debug something for a friend and this has me stumped and I don't know if
it's even possible.


Every object in C++ can be considered as an array of unsigned chars which
you can safely *read*. But you have to know the size. Example:


#include <iostream>
#include <vector>


int main()
{
using namespace std;

vector<int>something(10);

unsigned char *p=reinterpret_cast<unsigned char *>(&something);


// std::cout<<static_cast<unsigned>(something); if you want to see
numbers
for(unsigned i=0; i<sizeof(something); ++i)
cout<<p;

cout<<endl;

}


All bytes including those with padding bits are printed.






Ioannis Vranos
 
I

Ioannis Vranos

Ioannis Vranos said:
// std::cout<<static_cast<unsigned>(something); if you want to see
numbers



// std::cout<<static_cast<unsigned>(p); if you want to see numbers






Ioannis Vranos
 
C

Christopher Benson-Manica

Chris Cranford said:
The problem is i have no idea what the pointer really is to and nor do I
have any idea what the total size of the pointer is. I'm trying to
debug something for a friend and this has me stumped and I don't know if
it's even possible.

How can you have no idea what the pointer is? If it isn't a pointer
to a type you know about, then it's a void pointer. If it's a void
pointer, you 1) can't dereference it directly, and 2) can't
dereference it at all unless you figure out (preferably by reading the
code, although I guess you could static_cast randomly...) what it was
pointing to. And you can't "dump" what a pointer is pointing to
without dereferencing the pointer.
 
C

Christopher Benson-Manica

Christopher Benson-Manica said:
...
pointing to. And you can't "dump" what a pointer is pointing to
without dereferencing the pointer.
^
Or, of course, by knowing the size of what it's pointing to.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top