chrisd
August 15, 2019, 10:04pm
1
Are isize
and usize
guaranteed to be the same byte width as pointers? Or is there a possibility they can differ? E.g. when pointer widths are larger than than the addressable memory.
Here is the current stance of the Unsafe Code Guidelines Reference :
https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#isize-and-usize:
The isize
and usize
types are pointer-sized signed and unsigned integers. They have the same layout as the pointer types for which the pointee is Sized
, and are layout compatible with C's uintptr_t
and intptr_t
types.
Note : C99 7.18.2.4 requires uintptr_t
and intptr_t
to be at least 16-bit wide. All platforms we currently support have a C platform, and as a consequence, isize
/ usize
are at least 16-bit wide for all of them.
Note : Rust's usize
and C's unsigned
types are not equivalent. C's unsigned
is at least as large as a short, allowed to have padding bits, etc. but it is not necessarily pointer-sized.
Note : in the current Rust implementation, the layouts of isize
and usize
determine the following:
the maximum size of Rust allocations is limited to isize::max_value()
. The LLVM getelementptr
instruction uses signed-integer field offsets. Rust calls getelementptr
with the inbounds
flag which assumes that field offsets do not overflow,
the maximum number of elements in an array is usize::max_value()
( [T; N: usize]
. Only ZST arrays can probably be this large in practice, non-ZST arrays are bound by the maximum size of Rust values,
the maximum value in bytes by which a pointer can be offseted using ptr.add
or ptr.offset
is isize::max_value()
.
These limits have not gone through the RFC process and are not guaranteed to hold.
6 Likes
system
Closed
November 13, 2019, 10:40pm
3
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.