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.
Printable View
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;
}
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;
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.
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 doQuote:
Originally Posted by Salem
and 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.Quote:
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!Quote:
Originally Posted by Salem
The answer was already given by CornedBee: cast to void*.Quote:
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*.
Ok, usingQuote:
Originally Posted by dwks
requires me to make a function in Monitor with prototypeCode:mon << (void*)msg
but 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.
static_cast isn't a function. It's an operator. Not having it is like not having the '+' operator or the array index operator '[]'.
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;
}
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 coutQuote:
Originally Posted by Laserve
o rly, lets seeQuote:
Originally Posted by vart
maybe next time I wont try to be helpful :PQuote:
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.
Maybe next time you will read a little bit futher when just the first post in the thread? ;)Quote:
Originally Posted by Laserve
Quote:
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: