Getting the address of the object a pointer is pointing to

Started by
7 comments, last by Servant of the Lord 13 years, 6 months ago
For some reason, my mind is sleepy, and I can't remember how to, even after looking it up online.

Is this correct?
unsigned int address = (unsigned int)pointerToClass;

I'm trying to figure out if my pointer is getting corrupted... (I'm fairly certain it is).
Advertisement
What's your goal? A pointer IS the address of the object it's pointing to. Why would you want to cast it as an integer?

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

Quote:Original post by Servant of the Lord
I'm trying to figure out if my pointer is getting corrupted...


How is casting it to an integer going to help you find that out?
I'm trying to store the address for later viewing.
Basically, I have a class that retrieves a pointer on construction, and I want to store the address of the object it's pointing to.

Later, when the class is being destroyed, I again want to get the address of the object the pointer is pointing to.
myClass::myClass(){    this->pointerToClass = GetPointerFromFunction();    if(this->pointerToClass == NULL)        std::cout << "Pointer is NULL";    this->address = (unsigned int)this->pointerToClass;}myClass::~myClass(){    std::cout << "Original address: " << this->address;    std::cout << "Current address: " << (unsigned int)this->pointerToClass;    FreePointer(this->pointerToClass)    this->pointerToClass = NULL;}


I want to know if the pointer is getting corrupted during the lifetime of the class. To do this, I need to store the address of the object the pointer is pointing to.

I'm using QT, and Qt's altered version of gcc and gdb - but I have limited experience with gdb, so I'm using it as much as I can, but still can't figure out what's going on - my program keeps crashing in absurd spots... and I think I am recognizing the affects of a corrupt pointer.

I'm trying to avoid getting the address of the pointer itself, and instead getting the address of the object it's pointing to...
I was originally trying:
this->address = &(*this->pointer);
But it wouldn't compile.

If this doesn't make sense, I'm probably more drowsy than I think. [smile]
Good question. Assuming we're talking about C++ here (and not C99), there is no standard types that represent pointers as integers.

C99 has optional types intptr_t and uintptr_t, which are representations of pointers of integers. You have to be enable C99 extensions to C++ in your C++ compiler for this to work, as such it's not a reliable, conforming way to make this work.

Thus, as pointed out in this stackoverflow thread, you will have to either



[Edited by - Konfusius on September 26, 2010 3:20:02 PM]
Quote:Original post by Servant of the Lord
I'm trying to store the address for later viewing.


Sorry if I'm missing something, but why not just store it in another pointer?
@Konfusius:
Oh, hmm, for some reason I recall the very first C++ book I read, 4 years ago, teaching me I could store addresses of memory in integers. I didn't realize this was something complex.
Quote:Original post by Gage64
Quote:Original post by Servant of the Lord
I'm trying to store the address for later viewing.


Sorry if I'm missing something, but why not just store it in another pointer?

Because that would be the smart easy logical way?
Thanks, I'll do it like that. So what is the correct way to print out the address of a pointer? I can compare the two pointers together to see if they match, but how can I display the address they hold if I wanted to see it?

I also realized (while trying to wake myself up with a shower), that since I don't want the pointer to change, I should just have it in a reference anyway.
In C++ you can just use << with a stream and a void pointer. If you're feeling C'ish the %p format specifier can be used with memory addresses.
Thanks.

This topic is closed to new replies.

Advertisement