Storing pointer value in a string
What happens when we store pointer value in the string, and convert back to pointer and deference the pointer. I know that, we should not convert pointer to a string and again convert back. Can you please explain why the pointer value should not be stored in a string.
Re: Storing pointer value in a string
Mind explaining why you want to store the numeric representation of an address as a string in the first place?
Re: Storing pointer value in a string
I shall try to explain,
I have a module, which accepts a callback function pointer ((e.g) void callback(string)) and a string argument. I would like to pass my object pointer to the string argument and when the callback gets called, I can convert the string back to pointer and access the object directly.
I know it can't be done , but I don't understand the reason why? Can you please explain.
Re: Storing pointer value in a string
Off the top of my head I don't see why it wouldn't work. What happens when you try it?
Re: Storing pointer value in a string
Quote:
Originally Posted by
htonivi
I know it can't be done
Who told you that? Academically it can...
Code:
#include <iostream>
#include <string>
#include <sstream>
template <typename T>
std::string convertPointerToStringAddress(const T* obj)
{
int address(reinterpret_cast<int>(obj));
std::stringstream ss;
ss << address;
return ss.str();
}
template <typename T>
T* convertAddressStringToPointer(const std::string& address)
{
std::stringstream ss;
ss << address;
int tmp(0);
if(!(ss >> tmp)) throw std::runtime_error("Failed - invalid address!");
return reinterpret_cast<T*>(tmp);
}
int main()
{
int val(0);
int* ptr = &val;
std::cout << ptr << std::endl;
std::string address = convertPointerToStringAddress(ptr);
int* ptr2 = convertAddressStringToPointer<int>(address);
if (ptr==ptr2)
{
std::cout << "The pointers are the same!" << std::endl;
}
else
{
std::cout << "The pointers are different! : " << std::endl;
}
system("PAUSE");
}
But I would hate to see anything like the above in production code because it makes a mockery of type safety. If you "think" you need something like the above then I would suggest that your design is wrong and you need to rethink it.
Re: Storing pointer value in a string
Quote:
Originally Posted by
PredicateNormative
Who told you that? Academically it can...
FYI, you're making a deadly assumption about sizeof(int) and sizeof(T*)... :eek:
Re: Storing pointer value in a string
It crashes intermittently. Please note that the object When I changed this logic, the program works fine.
Re: Storing pointer value in a string
Quote:
Originally Posted by
Plasmator
FYI, you're making a deadly assumption about sizeof(int) and sizeof(T*)... :eek:
Thanks Plasmator. You are right - I have assumed that the two are of the same size, which although is true most of the time, it clearly is not true for LLP64 and LP64 data models. Therefore compilations under these 64 bit data models will experience issues when operating on addresses that map at values outside of 32 bits. On that note this code is clearly not portable and that is another reason for not using it in production code!
Re: Storing pointer value in a string
Quote:
Originally Posted by
htonivi
It crashes intermittently. Please note that the object When I changed this logic, the program works fine.
What crashes intermittently?
Re: Storing pointer value in a string
I am sorry for my poor English. The program crashes intermittently. Please note that, When I changed the logic (not assigning pointer value to a string), the program works fine.
Re: Storing pointer value in a string
A better solution would be to create a functor which has a handle to the object internally as the callback. Boost::bind could make this easy.
Re: Storing pointer value in a string
Quote:
Originally Posted by
Lindley
A better solution would be to create a functor which has a handle to the object internally as the callback. Boost::bind could make this easy.
Thanks for the alternate solution. But I would like to know, if storing pointer value in string is the cause of the crash.
Re: Storing pointer value in a string
Could well be. I think going the "correct" route is more important than figuring out how bad the hack is though.
Re: Storing pointer value in a string
Code:
ObjectType * Object = new ObjectType();
unsigned long i = &Object; //make sure there is room, most pointers are 64-bit
stringstream out;
out << i;
string s = out.str();
..
sscanf(s.c_str(), "%u", &i);
ObjectType * newObjectPtr = (ObjectType *) i;
Not really something you want to do, but you can. Also, the use of the C cast isn't recommended either.