strlen() on non-null-terminated char string?

Is strlen(const char *s) defined when s is not null-terminated, and if so, what does it return?


No, it is not defined. It may result in a memory access violation, as it will keep counting until it reaches the first memory byte whose value is 0.


From the C99 standard:

The strlen function returns the number of characters that precede the terminating null character.

If there is no null character that means the result is undefined.


May be You need strnlen?