Talk:cpp/types/ptrdiff t
Article change proposition (I'm not sure if it's correct, so, I will post it here for discussion instead of applying it myself):
The sentence:
"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."
Should it be:
If an array is so large (greater than PTRDIFF_MAX bytes, but less than SIZE_MAX bytes) ...
ptrdiff_t serves to keep result of subtracting a pointer from another one. The range of possible values resulting from such an operation depend on the length of the array in question, expressed in 'bytes'(char-s), not upon the length expressed in 'elements'. Thus, changing respective text to "greater than PTRDIFF_MAX bytes" would make overall statement correct. As for "but less than SIZE_MAX bytes" part, it points to the fact that no valid object is greater in size than SIZE_MAX bytes, so it seems correct if somewhat unnecessary. I'm not sure if I a'v been missing something out.
Cpprf acount (talk) 11:54, 3 November 2017 (PDT)
- The result of subtracting two pointers is, in fact, the number of elements, not the number of bytes. The statement is correct as written. T. Canens (talk) 12:51, 3 November 2017 (PDT)
[edit] array length stored in std::ptrdiff_t
The following is wrong:
"For char arrays shorter than PTRDIFF_MAX, std::ptrdiff_t
acts as the signed counterpart of std::size_t: it can store the size of the array of any type and is, on most platforms, synonymous with std::intptr_t."
Better is:
std::ptrdiff_t
acts as the signed counterpart of std::size_t: it can store the size of the array of any type and is, on most platforms, synonymous with std::intptr_t."
And add:
Arrays have a maximum object size of PTRDIFF_MAX, meaning that the difference between the starting elment and the element one-past-the-last-possible-valid-element is 0-PTRDIFF_MAX (equal to PTRDIFF_MIN+1); or if the order is swapped the subtraction is PTRDIFF_MAX-0. Both are validly representable in a std::ptrdiff_t
PS: https://wandbox.org/permlink/a3o3KP7vKOUGdEzF
- Notice that the standard specifically allows for the difference between two array indices to not be representable in a ptrdiff_t, and the maximum size of an object is simply representable in a size_t --Ybab321 (talk) 03:15, 13 March 2023 (PDT)