Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 16 Jul 2010 11:19:27 +0300
From:      Giorgos Keramidas <keramida@ceid.upatras.gr>
To:        deeptech71@gmail.com
Cc:        freebsd-chat@freebsd.org
Subject:   Re: is strlen()'s read-4-bytes-ahead a standard?
Message-ID:  <874ofzu9dc.fsf@kobe.laptop>
In-Reply-To: <AANLkTim9ULyOAPigkpxLC_5Lrs1fN6abkYN1DDmTxSvz@mail.gmail.com> (deeptech's message of "Fri, 16 Jul 2010 09:41:23 %2B0200")
References:  <AANLkTim9ULyOAPigkpxLC_5Lrs1fN6abkYN1DDmTxSvz@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 16 Jul 2010 09:41:23 +0200, "deeptech71@gmail.com" <deeptech71@gmail.com> wrote:
>Xin LI wrote:
>>On 2010/07/15 15:38, deeptech71@gmail.com wrote:
>>> 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.
>
> OK, can anyone confirm this?

The only text about strlen()'s behavior in my copy of the ISO/IEC
9899:1999 (E) standard is:

  |     7.21.6.3 The strlen function
  |
  |     Synopsis
  |
  | 1   #include <string.h>
  |     size_t strlen(const char *s);
  |
  |     Description
  |
  | 2   The strlen function computes the length of the string pointed
  |     to by s.
  |
  |     Returns
  |
  | 3   The strlen function returns the number of characters that
  |     precede the terminating null character.

There is no reference to *how* this may be implemented, so you can
safely assume that the usual "as if" rules of the C standard apply.
The underlying code may read a single character at a time, a word at a
time, or may even call system-specific kernel calls that do 'magic'
behind the scenes to compute the string length.

What _really_ matters, as far as the standard is concerned, is that
you get a meaningful return value that matches the current length of
the input string.




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?874ofzu9dc.fsf>