Sign in to follow this  
Servant of the Lord

Getting the address of the object a pointer is pointing to

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
@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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this