Archived

This topic is now archived and is closed to further replies.

HappyToLearnC

Viewing memory address

Recommended Posts

Hi What casting I should use to make a pointer of any type prints to a MessageBox second parameter. For example struct sFrame{} sFrame *sFrame1; MessageBox(NULL,sFrame1,""MB_OK); Well, I want to know the memory location for sFrame1. ManyThanks

Share this post


Link to post
Share on other sites
You need to create a string with the address, not do a cast (unless you use boost::lexical_cast).


  
#include <sstream>

struct Foo {};
Foo f;

int main()
{
// create an output string stream

std::ostringstream oss;

// write the address of f to the stream

oss << &f; // or possibly oss << (void*)&f;


// extract the string from the stream,

// then pass it as a const char* string to MessageBox

MessageBox( NULL, oss.str().c_str(), "", MB_OK );

// clears the flags in oss (if the object is reused)

oss.clear();

// clear the string in oss (if the object is reused)

oss.str("
");
};


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
Try using sprintf() and then passing the buffer that you got from that to the MessageBox().

I know printf() can do addresses so sprintf() should be able to also.

Er.. I just noticed the one above me says how to do it also...
Well, this is just a different way. Take your pick

[edited by - Soulkeeper on October 19, 2002 2:17:16 AM]

Share this post


Link to post
Share on other sites