for the following C code: [code] the return line can be UB if the string were extremely…
- User
for the following C code:
size_t strlen(const char * str) { const char *s; for (s = str; *s; ++s) {} return s - str; }the return line can be UB if the string were extremely long (longer than PTRDIFF_MAX). would "return (uintptr_t )s - (uintptr_t )str;" solve this UB? Can we use this cast in any other pointer subtraction situation?
- Assistant
Great question! Let's break it down:
1. Why is
s - strpotentially UB?In C, pointer subtraction (
s - str) yields a result of typeptrdiff_t, which is a signed integer type. The C standard (C99 §6.5.6/9) says:When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements.
But, if the difference is greater than
PTRDIFF_MAX, the result is undefined behavior (UB), because it cannot be represented in aptrdiff_t.2. Would casting to
uintptr_tsolve this?Your suggestion:
return (uintptr_t)s - (uintptr_t)str;This avoids UB from pointer subtraction overflow, because
uintptr_tis an unsigned integer type capable of holding any pointer value. The subtraction is now done in the unsigned integer domain, so it will wrap around moduloUINTPTR_MAX + 1if needed, which is well-defined.However, this is not a portable or standard-compliant way to compute the length of a string, because:
- The C standard only guarantees that pointer subtraction is valid within the same array object (or one past the end).
- Casting pointers to integers and subtracting them is not guaranteed to give the correct element count, especially if the pointers are not aligned or if the platform has "holes" in the address space.
- The result is a byte difference, not an element count. For
char *, this is fine, but for other types, it would be wrong.
3. Can you use this cast in other pointer subtraction situations?
No, not in general. This trick only works for
char *(orunsigned char *,uint8_t *, etc.), where the size of the element is 1 byte. For other types, pointer subtraction gives you the number of elements, not the number of bytes. Casting touintptr_tand subtracting gives you the number of bytes between the pointers, which is not the same as the number of elements for types larger than 1 byte.Example:
int arr[100]; int *a = &arr[10]; int *b = &arr[0]; ptrdiff_t diff = a - b; // 10 (number of elements) uintptr_t byte_diff = (uintptr_t)a - (uintptr_t)b; // 40 (if sizeof(int) == 4)4. Summary Table
Situation s - str(uintptr_t)s - (uintptr_t)strchar *Number of chars, UB if > PTRDIFF_MAX Number of bytes, always defined (modulo wrap) int *Number of ints, UB if > PTRDIFF_MAX Number of bytes, not number of ints! Different arrays UB Implementation-defined, but not meaningful 5. Conclusion
- For
strlen, casting touintptr_tavoids UB from overflow, but is not standard C and may not be portable. - For other pointer types, this cast does not give the correct element count.
- Pointer subtraction is only defined within the same array object (or one past the end).
If you need to handle extremely long strings (longer than
PTRDIFF_MAX), you need a different approach, such as returning a larger type (if your platform supports it), or using a loop that counts in chunks.
TL;DR:
(uintptr_t)s - (uintptr_t)stravoids UB for very long strings, but is not standard C and only gives the correct result forchar *. Do not use this trick for other pointer types or in general pointer arithmetic.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.