Boost logo

Boost :

Subject: Re: [boost] [AFIO] Formal review
From: Manu343726 (manu343726_at_[hidden])
Date: 2015-09-02 05:19:37


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.

--
View this message in context: http://boost.2283326.n4.nabble.com/AFIO-Formal-review-tp4679535p4679636.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk