size_t + ptrdiff_t

Artur Skawina art.08.09 at gmail.com
Sun Feb 19 11:21:55 PST 2012


On 02/19/12 17:23, Manu wrote:
> On 19 February 2012 18:03, Vladimir Panteleev <vladimir at thecybershadow.net <mailto:vladimir at thecybershadow.net>> wrote:
> 
>     On Sunday, 19 February 2012 at 15:26:27 UTC, Manu wrote:
> 
>         There is code that assumes size_t is the width of the pointer
> 
> 
>     When is this not true? I can only think of 16-bit far pointers.
> 
> 
> Ignoring small embedded systems (for which it is almost always true), some that immediately come to mind:
> 
>  NaCl (Google Native Client) - x64 arch, 32bit pointers ... <- of immediate concern to me
>  PPC based consoles; PS3, X360, Wii, WiiU (not released yet) - 64bit, all 32bit pointers
>  Android, and probably iOS; 64bit ARM chips - will certainly not fork the OS to use 64bit pointers

not to mention linux - x32 (https://sites.google.com/site/x32abi/)

But 'size_t' is the size of an object -- so sizeof(size_t)==sizeof(void*) is
a pretty safe assumption. It would be a bit hard to work with objects that are
larger than the address space covered by the pointer... Is any of the above
platforms using segmentation tricks and is sizeof(char*-char*)>sizeof(char*)?
I think you mean "native_int" - something that D is missing.

On 02/19/12 19:07, Timon Gehr wrote:
> Under which circumstances is it important to have a distinct type that denotes the register size? What kind of code requires such a type? It is unportable.

eg any time you don't want to artificially restrict the size to less than the
native one, not use a type wider than the hw efficiently handles or need C
compatibility.
Yes, you can use 'static if' and 'version' tricks, but that's inconvenient and
often obfuscates the code, so you end up reinventing c_int/c_long...
And that's not ideal either; having the right types [1] always predefined would
be much better.

On 02/19/12 18:26, Stewart Gordon wrote:
> If you want to know the size of an int, you would just use int.sizeof.  Problem solved.

Exactly. Except doing this for D's int would be kind of pointless, wouldn't it?...
With a native_int type you *can* write generic code and switch on native_int.sizeof. 

artur

[1] ie a signed/unsigned int that is large as the CPU registers allow. [2]
[2] and note that using anything smaller can result in performance degradation,
    if the values need to be converted to a full-width format.


More information about the Digitalmars-d mailing list