is strlen()'s read-4-bytes-ahead a standard?

Lowell Gilbert freebsd-chat-local at be-well.ilk.org
Fri Jul 16 14:21:30 UTC 2010


Dag-Erling Smørgrav <des at des.no> writes:

> Xin LI <delphij at delphij.net> writes:
>> deeptech71 at gmail.com writes:
>> > Some C implementations use the read-4-bytes-ahead technique to speed
>> > up strlen(). Does the C standard state anything about strlen() being
>> > allowed to read past the terminating zero?
>> It's not 4-bytes-ahead, but read a whole (aligned) word at one time.
>> I think C standard does not dictate in this detail.
>
> My guess is that it invokes undefined behavior, but it doesn't matter in
> practice, because as long as you only read one aligned word at a time,
> and as long as the pointer you got is valid and points to a properly
> terminated string, you might read trash (which is expected), but you
> will never read unmapped memory.

strlen() is part of the implementation, and doesn't have to worry about
undefined behaviour.  Any conforming *program* will work properly, and
that's all the standard library needs to ensure.


More information about the freebsd-chat mailing list