What do people use to denote that size_t is invalid? -1 does not work, and 0 can be a valid size.
5 Answers
Perhaps ((size_t)-1)
?
Strictly speaking, it is a valid size, but once you have this one you're not likely to need any other ;-)

- 138,757
- 24
- 193
- 173
-
1some of the C99 multibyte string functions do this: they return `(size_t)(-1)` or even `(size_t)(-2)` to signify abnormal conditions; it's not optimal, but it works in practice; if the reason for your question is error handling, it might be a better idea to pass a pointer to an error variable (eg an error flag of type `_Bool`, an error code of type `int` or an error message of type `const char *`) as additional argument – Christoph Sep 14 '09 at 11:52
-
Yeah, it's a common idiom. You may often see the `(time_t)-1` as well. – Michael Krelin - hacker Sep 14 '09 at 12:09
-
@MichaelKrelin-hacker: `(time_t)-1` is a little different; the standard specifically says that that's the value returned by `time()` if the current time can't be determined. And `time_t` is often a signed type, so `(time_t)-1` is typically 1 second before the epoch. – Keith Thompson Sep 23 '13 at 15:03
-
@KeithThompson, while not arguing with what you say further, I do not see how it makes `(time_t)-1` "a little different" for the scope of our narration ;) – Michael Krelin - hacker Sep 23 '13 at 21:10
-
1@MichaelKrelin-hacker: `(size_t)-1` is right at the upper bound of the range of type `size_t`. It's typically impossible to have an object that big, making it a suitable in-band signalling value. On the other hand, for the most common representation of `time_t` as a signed integer representing seconds since 1970, `(time_t)-1` is right in the middle of the representable range, making difficult to distinguish between an error condition and a legitimate time of 1960-12-31 23:59:59 UTC. – Keith Thompson Sep 23 '13 at 21:14
-
@KeithThompson, not arguing with that. But we were talking about a convention to signify error condition. But anyway, I see no disagreement here and your comments definitely add value, so we can leave it at that ;) – Michael Krelin - hacker Sep 23 '13 at 21:17
If you're talking about std::string, then size_t's invalid value is std::string::npos. Normally you shouldn't use -1 because a size_t is unsigned, and you can get failed comparisons on a compiler doing implicit conversions between types.
That being said, std::strings's npos is set to 0XFFFFFFFFFFFFFFFF... which is the binary equivallent of -1. It also evaluates to the maximum allowed value for an unsigned size_t field.

- 10,944
- 6
- 56
- 81
-
1
-
1
-
1What is `std::string`, other than a syntax error? (Hint: The question is tagged "c".) – Keith Thompson Sep 23 '13 at 15:05
-
Basically you can not. Whatever value you use might be a valid one. Better pass a flag saying that it is invalid.

- 74,600
- 47
- 176
- 233
And what do you do to denote that an int
is invalid? -1 is a valid value for an int. These types don't have designated "invalid" values. You can decide to choose a certain value (that can never normally be the value of what your variable represents) to represent an illegal value, but that is your own definition, and not something that people generally use.
Personally, I don't like this way. I prefer to create another variable, bool IsValid
, which will say, whether the value of that size_t variable is valid. Sometimes, it may even be better to create a class to encapsulate them.

- 26,650
- 27
- 89
- 114
My version is:
#include <limits>
#define invalid_index std::numeric_limits<size_t>::max()

- 111
- 1
- 3
-
3`std::numeric_limits
::max()` does not compile in `C` as this question is tagged. – chux - Reinstate Monica May 09 '16 at 22:14