I need to pass a pointer through a scripting language which just has a double and string type, for this I only have to worry about 32-bit pointers.
Seeing as the pointers are 32-bit, I figured doubles had enough precision to safely store the pointer, which works, however the problem arises with some pointers to stream, presumably due to multiple inheritance...and I'm really not sure how to solve it.
Basically I've been casting the pointers to unsigned
, and then to double
. Then to get them back I've cast the double
to unsigned
and then to whatever type the pointer is.
e.g.:
double example()
{
int *i = new int[100];
return (double)(unsigned)i;
}
double example2(double i)
{
doSomething((int*)(unsigned)i);
}
However with the stream types it seems to not work...
std::ofstream *fs = new std::ofstream("example.txt");
std::cout << fs << std::endl;//029D1DF8 for example
double d = (double)(unsigned)fs;
std::ios *p = (std::ios*)(unsigned)d;
std::cout << p << std::endl;//029D1DF8 same thing seems fine
std::cout << ((std::ios*)fs) << std::endl;//029D1E54, opps, apparently a cast to std::ios changes the pointer to some offset!
Is there some way around this? I have got an idea using a map and id numbers but I'd rather avoid the cost of having such maps which may contain 1000's of entries. I much rather get the casting to work for this...