Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

size_t ptrdiff_t and address space

On my system both ptrdiff_t and size_t are 64-bit.

I would like to clarify two things:

  • I believe that no array could be as large as size_t due to address space restrictions. Is this true?

  • If yes, then, is there a guarantee that ptrdiff_t will be able to hold the result of subtraction of any pointers within the max-sized array?

like image 851
Constantineous Avatar asked Aug 20 '18 16:08

Constantineous


1 Answers

No, there is no such guarantee. See, for example, here: https://en.cppreference.com/w/cpp/types/ptrdiff_t

If an array is so large (greater than PTRDIFF_MAX elements, but less than SIZE_MAX bytes), that the difference between two pointers may not be representable as std::ptrdiff_t, the result of subtracting two such pointers is undefined.

like image 197
SergeyA Avatar answered Oct 06 '22 00:10

SergeyA