I want to use addresses of objects, as unique identifier, when I am storing the state of my simulation in a json file. I therefore convert the pointer to my object into a uintptr_t
.
Cat myCatptr = new Cat;
uintptr_t address= (uintptr_t)myCatptr;
Now I convert the address
into a QString:
QString str = QString::number(address);
I can then easily add the QString to a QJsonObject. But how can I convert back from QString to uintptr_t?
On my machine I know that sizeof(unsigned long long)
and sizeof(uintptr_t )
are both 8byte. Hence I can use
address= (uintptr_t)str.toULongLong();
Is this always correct, independent of the platform? This would require that
sizeof(uintptr_t) <= sizeof(unsigned long long)
for all platforms.
As stated here, sizeof(unsigned long long) is always >= 8byte
.
Again in summary:
Is my Ansatz, converting it to unsigned long long and then to uintptr_t always working?
If not, do you know an alternative that works on any platform?