Suppose I have two pointers to type T:
T* first = ...// whatever
T* second = ... //whatever else
Can I be sure that the distance between those two pointers can never exceed:
((size_t)(-1))/sizeof(T)?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can only compute the distance between two pointers (subtract one pointer from another) if both pointers point to elements in the same array, or to one-past-the-end of the same array.
If the two pointers meet that constraint, then yes, the absolute value of the difference between the two pointers cannot exceed
((size_t)(-1)) / sizeof(T)becausesize_tmust be wide enough to represent the size of any object in bytes.If the two pointers do not meet that constraint, then there’s no guarantee at all.