Re: How big is an unsigned long in Linux compiled by GCC for a 64-bit machine?

James Kuyper <[email protected]> Fri, 2 Apr 2021 19:45:25 -0400
Newsgroups alt.books.pratchett
Organization A noiseless patient Spider
Message-ID <[email protected]>
On 4/2/21 5:07 PM, Alan Mackenzie wrote:
> Alan Mackenzie <[email protected]> wrote:
>> David Brown <[email protected]> wrote:
> 
> [ .... ]
> 
>>> It is not generally a good idea to cast pointers back and forth to
>>> "unsigned long".  If you need to cast pointers to an integer type,
>>> prefer to use C99 standard types "uintptr_t" (or possibly "intptr_t") -
>>> that's what those types are for.  Linux headers may also provide other
>>> types for the purpose, in which case they may be preferable.
...
>> OK, thanks, that's good to know.  In any amendments I succeed in making,
>> I certainly won't be introducing any new "unsigned long"s which are
>> really pointers.
> 
> And I'm now eating my words, having done precisely that.  ;-)  The
> reason these things are defined as unsigned longs is because they are
> frequently subtracted from eachother, then divided by 2, and things like
> that.  With pointers declared as pointers, they have to be cast
> repeatedly to UL just to do the arithmetic.  I hate casts!.  So implicit
> conversions it is.

As he said above, that's still not a justification for using unsigned
long. You should use uint_ptr_t for such work.