I'm learning the Rustonomicon and I find this sentence on Implementing Vec -> Allocating confusing: (Note: zero-sized types are ignored below, and I'm not going to deal with them for the time being)
However in order to avoid subtle corner cases where someone reinterprets some array of
< isize::MAX
objects as bytes, std limits all allocations toisize::MAX
bytes.
My understanding is that it says:
- If a user of a type
T<A>
(whereA
has size a > 1) somehow reinterprets the type asT<B>
(whereB
has size 1), thenT<B>
might think the number of elements it has is a times the number of elementsT<A>
has. - If std didn't limit all allocations to
isize::MAX
bytes,T<B>
might think it has more thanisize::MAX
elements, which might cause index overflow when usingptr::offset
.
Is this correct? Can someone show me an example?