-
March 9th, 2009, 07:40 AM
#1
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.
-
March 9th, 2009, 07:42 AM
#2
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?
-
March 9th, 2009, 08:30 AM
#3
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.
-
March 9th, 2009, 09:09 AM
#4
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?
-
March 9th, 2009, 09:22 AM
#5
Re: Storing pointer value in a string
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.
-
March 9th, 2009, 09:43 AM
#6
Re: Storing pointer value in a string
Originally Posted by PredicateNormative
Who told you that? Academically it can...
FYI, you're making a deadly assumption about sizeof(int) and sizeof(T*)...
-
March 9th, 2009, 10:02 AM
#7
Re: Storing pointer value in a string
It crashes intermittently. Please note that the object When I changed this logic, the program works fine.
-
March 9th, 2009, 10:46 AM
#8
Re: Storing pointer value in a string
Originally Posted by Plasmator
FYI, you're making a deadly assumption about sizeof(int) and sizeof(T*)...
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!
-
March 9th, 2009, 10:47 AM
#9
Re: Storing pointer value in a string
Originally Posted by htonivi
It crashes intermittently. Please note that the object When I changed this logic, the program works fine.
What crashes intermittently?
-
March 9th, 2009, 11:06 AM
#10
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.
-
March 9th, 2009, 11:17 AM
#11
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.
-
March 9th, 2009, 11:45 AM
#12
Re: Storing pointer value in a string
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.
-
March 9th, 2009, 12:33 PM
#13
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.
Last edited by Lindley; March 9th, 2009 at 12:41 PM.
-
March 9th, 2009, 12:34 PM
#14
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|