On Tue, 16 Jun 2015 21:37:00 +0100
Chris Vine <chris@cvine--nospam--.
freeserve.co.uk> wrote:
[snip]
> > Too ,bad ;(
> > I need intptr_t as conversion is actually passing int parameter as
> > void* and than back to int.
>
> This is a common idiom in C libraries, and if you are doing the
> conversions that way, and all your numerical manipulation is done
> using ints, you don't need to worry about whether the size of void*
> is larger than the size of int. If you begin with a value in the
> range of int you will end with a value within the range of int and
> the conversion will work. (You do have to worry if the size of int
> is larger than the size of void*, but I know of no implementation
> where that is the case.)
On looking it up I think on the last point I was too pessimistic. It
seems to me that reinterpret casts of int->void*->int are obliged to
work according to the standard - in other words, sizeof(int) cannot be
larger than sizeof(void*) in a conforming implementation. In fact, it
appears that sizeof(long long) cannot be larger than sizeof(void*):
§5.2.10/4 of C++11: "A pointer can be explicitly converted to any
integral type large enough to hold it. The mapping function is
implementation-defined. [ Note: It is intended to be unsurprising to
those who know the addressing structure of the underlying machine. —
end note ]. ... ."
§5.2.10/5 of C++11: "A value of integral type or enumeration type can
be explicitly converted to a pointer. A pointer converted to an
integer of sufficient size (if any such exists on the implementation)
and back to the same pointer type will have its original value;
mappings between pointers and integers are otherwise
implementation-defined. ... ."
In the first sentence of §5.2.10/5 there is no qualification about sizes
for the integer->pointer conversion. The reverse conversion does have a
size qualification.
Chris