4 ms·
I'm not confident it's valid to allocate objects larger than SIZE_MAX/2 on any platform.
by loeg 4y ago
I'm not confident it's valid to allocate objects larger than SIZE_MAX/2 on any platform.
- klodolph 4y agoWhy not? The first argument that comes to mind is the existence of ptrdiff_t, but you’re not guaranteed to get a valid ptrdiff_t just by subtracting two pointers in the same array!
- teo_zero 4y agoNot objects but memory. It was common in 16-bit era. And you would access it through memory segmentation, with two registers: one pointing at the segment beginning, the other being the offset. In code: addr = base << 4 + offset
- jwilk 4y agoAs a data point, C23 added explicit wording about overflow handling in calloc(): > The calloc function returns […] a null pointer […] if the mathematical product of nmemb * size is not representable as a value of type size_t. So apparently the Committee is OK with allocating more than SIZE_MAX/2 (but not more than SIZE_MAX).