Hi, how can I get the actual memory address (f.instance 0x0800h) where my variables are stored and my pointers are pointing?
I'd like to print the address to the screen using cout.
Hi, how can I get the actual memory address (f.instance 0x0800h) where my variables are stored and my pointers are pointing?
I'd like to print the address to the screen using cout.
Code:#include <iostream> using namespace std; int main() { int iVal1 = 32; cout << &iVal1 << endl; int* ptrVal2 = new int(128); cout << ptrVal2 << endl; delete ptrVal2; return 0; }
Last edited by rynoon; 01-07-2007 at 04:35 PM.
Courage, my friends; 'tis not too late to build a better world. - Tommy Douglas
If the pointer points to char, you have to cast it to a void*, though, else it's interpreted as a string.
Code:const char *ptr = "some literal"; std::cout << static_cast<void*>(ptr) << std::endl;
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Thanks for your replies.
The reason I'm asking it that I've written a class, Monitor, which behaves kinda like cout. Today I'm able to do this:
but when I do this:Code:Monitor mon; int number = 123; mon << "A long string"; mon << number;
I get an "error: invalid conversion from 'char**' to 'int'" when I try to compile, so I guess I need to create a Monitor& Monitor::operator<<(char**) function as well, but I'm uncertain how to write it so that the address is printed.Code:Monitor mon; char* msg = "A long string"; mon << "Address of msg is: "; mon << &msg;
Part of my Monitor class looks like this:
I've also got a function Monitor% Monitor::operator<<(int data), so I can print integers.Code:Monitor& Monitor::operator<<(char *data) { while (*data != 0) { if (*data == '\n') //new line { while(this->characterPosition < 80) //Insert whitespaces until we are at end of line { PutCharacter(' '); } //data++; } else if(*data == '\t') //tabulator { int tabulatorSize = 4; while(tabulatorSize--) this->PutCharacter(' '); } this->PutCharacter(*data); data++; } return *this; }
Given your function, it would just be
mon << msg;
> while(this->characterPosition < 80)
How does this loop exit, given that nothing changes inside the loop which could ever make it false.
> while(tabulatorSize--)
Mmm, if I have
" \tHello"
and
"\tHello"
I would normally expect the two Hellos to be lined up.
Tabs are a bit more complicated than a simple replacement with 4 (or however many) spaces.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
That just outputs the actual characterstring "A long string", while I'm interrested in the address where the string is stored. I'd like to print for example 0x0203940. I tried to doOriginally Posted by Salemand have written a function like thisCode:mon << &msg;
but I'm still not able to output the actual address.Code:Monitor& Monitor::operator<<(char** data) { this->PutCharacter('0'); this->PutCharacter('x'); while (**data != 0) { this->PutCharacter(**data); **data++; } }
In the function PutCharacter I write a single char to the screen, and increment the characterPosition. So I get out of the loop eventually.Originally Posted by Salem
You're right, I need to change that one so that it jumps to the first characterPosition which is dividable by 4. F.instance, if at position 38, I need to jump to position 40. Thanks!Originally Posted by Salem
The answer was already given by CornedBee: cast to void*.Originally Posted by Scalpel78
I've heard that some implementations of ostream don't overload void*, though. For those you might try casting to unsigned long*.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Ok, usingOriginally Posted by dwks
requires me to make a function in Monitor with prototypeCode:mon << (void*)msgbut then I'm very uncertain as to what the implementation needs to be.Code:Monitor& Monitor::operator<<(void*) { //What to put here? }
Let me also note, that I'm not using the standard library (I'm playing around with OS development and is developing EVERYTHING from scratch. That's why I need my own cout replacement), so I don't have the static_cast function.
I guess one way is to cast your void* to an unsigned long, then do whatever you did for your int function, but using base 16 rather than base 10.
Though this is far from being portable if you're doing it all yourself.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
static_cast isn't a function. It's an operator. Not having it is like not having the '+' operator or the array index operator '[]'.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
So what you want to know is how to convert a void* into a readable representation, right?
Salem gave you the right idea, but unsigned long is a dangerous type to use: it will fail, for example, on 64-bit Windows using the MS compiler.
Personally, I'd just use sprintf.
This allows you to avoid worrying about finding an integral type the same size as a pointer, which is not exactly trivial unless you accept non-portable code.Code:#include <climits> // Contains CHAR_BITS, I think. #include <cstdio> Monitor& Monitor::operator<<(void *ptr) { char buffer[sizeof(ptr) * ((CHAR_BITS + 3) / 4) + 1]; // A buffer that is guaranteed to be large // enough to hold the hex representation of a pointer, plus a NUL. std::sprintf(buffer, "%p", ptr); return (*this) << buffer; }
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
to format to hexadecimal immediately:
std::cout << std::hex << (void *)pointer << std::endl;
The question was how to implement this operator in its own class, not how to use with the coutOriginally Posted by Laserve
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
o rly, lets seeOriginally Posted by vart
maybe next time I wont try to be helpful :PHi, how can I get the actual memory address (f.instance 0x0800h) where my variables are stored and my pointers are pointing?
I'd like to print the address to the screen using cout.
Maybe next time you will read a little bit futher when just the first post in the thread?Originally Posted by Laserve
The reason I'm asking it that I've written a class, Monitor, which behaves kinda like cout. Today I'm able to do this:
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler