For example:
int* x = new int;
int y = reinterpret_cast<int>(x);
y now holds the integer value of the memory address of variable x.
Variable y is of size int. Will that int size always be large enough to store the converted memory address of ANY TYPE being converted to int?
EDIT:
Or is safer to use long int to avoid a possible loss of data?
EDIT 2: Sorry people, to make this question more understandable the thing I want to find out here is the size of returned HEX value as a number, not size of int nor size of pointer to int but plain hex value. I need to get that value in in human-readable notation. That’s why I’m using reinterpret_cast to convert that memory HEX value to DEC value. But to store the value safely I also need to fing out into what kind of variable to it: int, long – what type is big enough?
No, that’s not safe. There’s no guarantee
sizeof(int) == sizeof(int*)On a 64 bit platform you’re almost guaranteed that it’s not.
As for the “hexadecimal value” … I’m not sure what you’re talking about. If you’re talking about the textual representation of the pointer in hexadecimal … you’d need a string.
Edit to try and help the OP based on comments:
Because computers don’t work in hex. I don’t know how else to explain it. An
intstores some amount of bits (binary), as does a long. Hexadecimal is a textual representation of those bits (specifically, the base16 representation). strings are used for textual representations of values. If you need a hexadecimal representation of a pointer, you would need to convert that pointer to text (hex).Here’s a c++ example of how you would do that:
test.cpp
Sample output:
It’s worth noting that the same thing is needed when you want to see the base10 (decimal) representation of a number – you have to convert it to a string. Everything in memory is stored in binary (base2)