array-size/malloc limit and strlen() failure

S

Stephen Sprunk

James Kuyper said:
On 04/02/2014 02:31 AM, jay wrote: [...]
I read about strlen() in C standard (n1570) in 6.24.6.3, it does not
have any error condition associated with it.

Correct, strlen() provides no way of reporting errors. There's only one
possible error: passing it a pointer to a block of memory that is not
terminated by a null character. strlen() will continue looking for
characters until it reaches the end of that block of memory - what it
does after that point depends upon how your system works; as far as the
C standard is concerned, the behavior is simply undefined.

There are other possible errors: Passing it a null pointer, and
passing it an invalid non-null pointer.

Implementations of strlen are not required to detect any of these
errors; they all have undefined behavior. (It would be easy to
detect a null pointer argument, but the overhead might be an issue,
and on most systems it's likely to cause a run-time fault anyway
-- and it's not clear what it should return for a null pointer
argument anyway.)

This exact scenario was my first exposure to portability problems and
the need to know what C actually guarantees vs. what a given
implementation happens to do: strlen(NULL) returned 0 on AIX but
segfaulted on Linux.

S
 
M

Malcolm McLean

Defining strlen to return 0 would encourage treating both "" and
NULL as empty strings. If you wanted to *consistently* change the
language and library so that a null pointer always acts like an
empty string, I suppose you could, but I think it would be overly
complicated and would, on many systems, impose run-time overhead
without much benefit.
That used to be the case. In ancient C, address zero was always a zero, so
strlen(NULL) would return 0. But it's a philosophical problem really. Is
there a difference between no string of sausages and a string of no sausages?
 
I

Ike Naar

That used to be the case. In ancient C, address zero was always a zero, so
strlen(NULL) would return 0. But it's a philosophical problem really. Is
there a difference between no string of sausages and a string of no sausages?

Not sure about the string of sausages; for a string of characters
there is a difference: an empty string has a terminating '\0' character.
 
K

Keith Thompson

Malcolm McLean said:
That used to be the case. In ancient C, address zero was always a zero, so
strlen(NULL) would return 0.

That was true in some old C *implementations*. I've never heard that it
was ever true of the language.
But it's a philosophical problem really. Is
there a difference between no string of sausages and a string of no sausages?

Yes, one is a string and the other isn't. And as Ike Naar points
out, a null pointer doesn't point to an array terminated by a null
character. An empty string is not an empty array; it must have at
least one element.
 
K

Kenny McCormack

That used to be the case. In ancient C, address zero was always a zero, so
strlen(NULL) would return 0.

That was true in some old C *implementations*. I've never heard that it
was ever true of the language.
http://flamewarriorsguide.com/warriorshtm/android.htm
But it's a philosophical problem really. Is
there a difference between no string of sausages and a string of no sausages?

Yes, one is a string and the other isn't. And as Ike Naar points
out, a null pointer doesn't point to an array terminated by a null
character. An empty string is not an empty array; it must have at
least one element.[/QUOTE]

http://flamewarriorsguide.com/warriorshtm/android.htm

--
Modern Christian: Someone who can take time out from
complaining about "welfare mothers popping out babies we
have to feed" to complain about welfare mothers getting
abortions that PREVENT more babies to be raised at public
expense.
 
S

Stefan Ram

Ike Naar said:
Not sure about the string of sausages; for a string of characters
there is a difference: an empty string has a terminating '\0' character.

»The strlen function computes the length of the string
pointed to by s.« [N1570, 7.24.6.3]

»strlen( "" )« is 0. Thus, the length of the string pointed
to by »""« is 0. Thus, this string does /not/ contain the
terminating '\0' character, otherwise its length would be 1.
 
J

James Kuyper

Ike Naar said:
Not sure about the string of sausages; for a string of characters
there is a difference: an empty string has a terminating '\0' character.

»The strlen function computes the length of the string
pointed to by s.« [N1570, 7.24.6.3]

»strlen( "" )« is 0. Thus, the length of the string pointed
to by »""« is 0. Thus, this string does /not/ contain the
terminating '\0' character, otherwise its length would be 1.

7.1.1p1: "A _string_ is a contiguous sequence of characters terminated
by and including the first null character. ... The _length of a string_
is the number of bytes preceding the null character ..."

The underscores around the phrases "string" and "length of a string"
serve to indicate that those phrases are in italics in the original
text, which is an ISO convention indicating that the sentence each
italicized phrase occurs in serves as a definition for that phrase.

Note in particular the phrases "including" and "preceding" - because of
those phrases, the length of a string is exactly one less than the
number of bytes required to store the string. An empty string therefore
has a length of 0 and requires one byte to store it. When there is no
string, no bytes are required to store it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top