How is strlen implemented?

P

pete

Tim Rentsch wrote:
Normally I expect 'for' statements are used when iterating over known
quantities; also they usually "do" something with each element
iterated over. Of course these conditions needn't be true but most
often they are. So the for loop here seems a little off.
On the other hand, 'while' statements are often used to establish
postconditions. The code
which is more or less the definition for 'n' being the length of the
string 's'. (Initializing 'n' on its declaration is just a convenient
shortening of an initializing expression.)

Certainly you're right that operationally the two functions are
equivalent. It just seems to be a little more mental effort to be
sure that the 'for' code is doing the right thing - it's less clear
or less obvious or perhaps both. For these reasons I tend to favor
the 'while' form here.

My preferences for while loop vs. for loop, tends to go
according to aesthetics which I don't consider to be
related to style, in the sense that "good programming style"
means enhanced maintainability.

I think that for loops look funny when they have
empty expressions or statements.

I think your originally posted function definition
was only busy enough to fill up a while loop.
while( s[n] ) n++;

My preference for a loop to do something N times,
is a count down while loop.

void do_something_N_times(unsigned n)
{
while (n-- != 0) {
/* do something */
}
}

Always using a compound statement as a loop body,
*is* something that I consider to be a style issue.
 
M

Michael Wojcik

Y'know, I'm a bit disappointed no one's proposed the elegant:

static size_t strlen_c(const char *s, size_t c){
return *s? strlen_c(s+1, c+1) : c;
}

size_t strlen(const char *s) {
return strlen_c(s, 0);
}

:)

If your implementation optimizes tail recursion, this could even
produce reasonable code (for a normal C routine; usually for strlen
the implementor has other, better tricks available). Sometimes I
miss Scheme.

--
Michael Wojcik (e-mail address removed)

I will shoue the world one of the grate Wonders of the world in 15
months if Now man mourders me in Dors or out Dors
-- "Lord" Timothy Dexter, _A Pickle for the Knowing Ones_
 
R

Richard Tobin

Michael Wojcik said:
Y'know, I'm a bit disappointed no one's proposed the elegant:

static size_t strlen_c(const char *s, size_t c){
return *s? strlen_c(s+1, c+1) : c;
}

size_t strlen(const char *s) {
return strlen_c(s, 0);
}

Because it's not elegant!

size_t strlen(const char *s)
{
return *s ? strlen(s+1) + 1 : 0;
}

would be elegant, but unfortunately not tail-recursive.

-- Richard
 
M

Michael Wojcik

Because it's not elegant!

size_t strlen(const char *s)
{
return *s ? strlen(s+1) + 1 : 0;
}

would be elegant, but unfortunately not tail-recursive.

"Would be elegant"? Please! It is or (as in your example) is not.
I would be phenomenally wealthy, but unfortunately I don't possess
absurd amounts of money.

On the other hand, my strlen_c is elegant, and strlen is an elegant
wrapper for it. In accordance with the property of distributive
elegance, that makes my entire proposal elegant.

(This is probably sufficiently silly now.)

On a more serious note, I think continuation-passing style is quite
elegant, when done right. What do you have against it?
 
P

pete

Michael said:
"Would be elegant"? Please! It is or (as in your example) is not.
I would be phenomenally wealthy, but unfortunately I don't possess
absurd amounts of money.

On the other hand, my strlen_c is elegant, and strlen is an elegant
wrapper for it. In accordance with the property of distributive
elegance, that makes my entire proposal elegant.

(This is probably sufficiently silly now.)

On a more serious note, I think continuation-passing style is quite
elegant, when done right. What do you have against it?

It seems overly complicated.

size_t strlen(const char *s)
{
return *s ? strlen(s + 1) + 1 : 0;
}
 
R

Richard Tobin

On a more serious note, I think continuation-passing style is quite
elegant, when done right. What do you have against it?

I think it's wonderful. In fact, I like compilers that translate all
my code into it.

-- Richard
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top