← Back to context

Comment by pjmlp

2 years ago

If that is ever a possible issue, you switch the implementation to use two pointers.

    struct String { const char *buf, const char *buf_end; };

Actually I was answering this question wrong because I somehow understood it in the context of snprintf() return int, and I should have just replied "you can switch to size_t if you like". start + end pointer is certainly not necessary, not sure why one would ever do this. It's more inviting of bugs compared to start pointer + length.

  • It is how many languages implement strings without being bound by numeric limits.

    Naturally for this to work out without bugs, it cannot be exposed directly, only manipulated via a string library.

    • size_t is large enough to hold the size of the largest possible object in memory. In practice, on most architectures, that means it is the same size as pointers. I'm not sure if there is a case where start + end pointer can describe a valid string in memory that start pointer + size couldn't? If that was the case, that string wouldn't be an "object" by definition.