On 9/2/2015 4:19 AM, Manu343726 wrote:
Niall Douglas wrote
On 31 Aug 2015 at 20:39, Andrey Semashev wrote: Please do correct me if I am wrong, but I had thought that this is defined behaviour:
int a=5; void *b=(void *)(size_t) a; int c=(int)(size_t) b; assert(c==a);
This is certainly a very common pattern in C.
Although it's a common pattern, it's not correct. It has UB since the standard doesn't guarantee that sizeof(void*) == sizeof(int). The issues arise in 64 bit systems where memory addresses are 64 bits wide and int is 32 bits wide. The safe way to handle integer conversion of pointers is through std::intptr_t, which only purpose is to give an integer type guaranteed to have same width as ptrs.
I don't see how a 64bit pointer can't store a 32bit integer. He's not converting some arbitrary void* to an int in the third line. He knows it is actually a 32bit int stored in a void*.