473,785 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How is strlen implemented?

roy
Hi,

I was wondering how strlen is implemented.
What if the input string doesn't have a null terminator, namely the
'\0'?
Thanks a lot
Roy

Nov 14 '05
66 7789
Lawrence Kirby wrote:
On Thu, 28 Apr 2005 16:58:56 -0400, Joe Wright wrote:

.... snip ...

Show us a case where (p - s) can be out-of-bounds.


Instead think of a 16 bit system where size_t and ptrdiff_t are
16 bits wide. It would be permissible for a string to be up to
65534 characters plus the null character on that implementation
but anything above 32767 can cause problems for ptrdiff_t. 32768
is long for a string but not beyond the bounds of possibility.

size_t must be able to represent the size of any object (although
some debate is possible for calloc()). However C provides no
corresponding guarantee that ptrdiff_t can represent the
difference of any 2 pointers to elements of the same array.


Alright, you have finally convinced me. So this means that strlen
actually has to be a system function.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #51
CBFalconer <cb********@yah oo.com> writes:
Lawrence Kirby wrote:
On Thu, 28 Apr 2005 16:58:56 -0400, Joe Wright wrote:

... snip ...

Show us a case where (p - s) can be out-of-bounds.


Instead think of a 16 bit system where size_t and ptrdiff_t are
16 bits wide. It would be permissible for a string to be up to
65534 characters plus the null character on that implementation
but anything above 32767 can cause problems for ptrdiff_t. 32768
is long for a string but not beyond the bounds of possibility.

size_t must be able to represent the size of any object (although
some debate is possible for calloc()). However C provides no
corresponding guarantee that ptrdiff_t can represent the
difference of any 2 pointers to elements of the same array.


Alright, you have finally convinced me. So this means that strlen
actually has to be a system function.


No, it just means that the (p - s) method isn't strictly portable.

size_t strlen(const char *s)
{
size_t result = 0;
const char *p = s;
while (*p++ != '\0') {
result ++;
}
return result;
}

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #52
Keith Thompson wrote:

CBFalconer <cb********@yah oo.com> writes:
Lawrence Kirby wrote:
On Thu, 28 Apr 2005 16:58:56 -0400, Joe Wright wrote:

... snip ...

Show us a case where (p - s) can be out-of-bounds.

Instead think of a 16 bit system where size_t and ptrdiff_t are
16 bits wide. It would be permissible for a string to be up to
65534 characters plus the null character on that implementation
but anything above 32767 can cause problems for ptrdiff_t. 32768
is long for a string but not beyond the bounds of possibility.

size_t must be able to represent the size of any object (although
some debate is possible for calloc()). However C provides no
corresponding guarantee that ptrdiff_t can represent the
difference of any 2 pointers to elements of the same array.


Alright, you have finally convinced me. So this means that strlen
actually has to be a system function.


No, it just means that the (p - s) method isn't strictly portable.

size_t strlen(const char *s)
{
size_t result = 0;
const char *p = s;
while (*p++ != '\0') {
result ++;
}
return result;
}


One should bear in mind that we all know where to find
real strlen when we need it, and that these posted strlen
defintions aren't meant to be competitive in terms of performance.

Writing standard library functions in C,
and merely getting it right, brings up various C topics.

Do you not like to increment the s parameter directly?
Some people don't like to change the values of parameters.
I prefer to change them whenever it's handy.

size_t strlen(const char *s)
{
size_t n;

for (n = 0; *s != '\0'; ++s) {
++n;
}
return n;
}

--
pete
Nov 14 '05 #53
pete <pf*****@mindsp ring.com> writes:
Keith Thompson wrote: [...]
size_t strlen(const char *s)
{
size_t result = 0;
const char *p = s;
while (*p++ != '\0') {
result ++;
}
return result;
}


One should bear in mind that we all know where to find
real strlen when we need it, and that these posted strlen
defintions aren't meant to be competitive in terms of performance.


Of course.
Writing standard library functions in C,
and merely getting it right, brings up various C topics.
Sure. The issue (or at least *an* issue) is why certain functions are
included in the C standard library. In many cases it's just arbitrary
historical precedent; a C library designed from scratch would probably
look very different from what we have now. Some functions are in the
C library because they can't be implemented portably (and library
implementers are not constrained to write portable code); the
offsetof() macro is a good example, as are most of the functions in
<stdio.h>. Other functions are in the standard library just because
they're convenient. Many of them *can* be implemented perfectly
portably, but it's nice that not every program has to provide its own
strlen() function -- and in some cases the implementer can provide a
non-portable version with improved performance.
Do you not like to increment the s parameter directly?
Some people don't like to change the values of parameters.
I prefer to change them whenever it's handy.

size_t strlen(const char *s)
{
size_t n;

for (n = 0; *s != '\0'; ++s) {
++n;
}
return n;
}


Yes, that's a good solution (probably a little better than mine).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #54
pete wrote:
.... snip ...
One should bear in mind that we all know where to find real
strlen when we need it, and that these posted strlen defintions
aren't meant to be competitive in terms of performance.

Writing standard library functions in C,
and merely getting it right, brings up various C topics.

Do you not like to increment the s parameter directly?
Some people don't like to change the values of parameters.
I prefer to change them whenever it's handy.

size_t strlen(const char *s)
{
size_t n;

for (n = 0; *s != '\0'; ++s) {
++n;
}
return n;
}


Even so, I think I would prefer to write:

inline size_t strlen(const char *s)
{
size_t n;

for (n = 0; *s++;) ++n;
return n;
}

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #55
pete <pf*****@mindsp ring.com> writes:
Keith Thompson wrote:
[how might strlen be implemented portably...]

size_t strlen(const char *s)
{
size_t result = 0;
const char *p = s;
while (*p++ != '\0') {
result ++;
}
return result;
}


One should bear in mind that we all know where to find
real strlen when we need it, and that these posted strlen
defintions aren't meant to be competitive in terms of performance.

Writing standard library functions in C,
and merely getting it right, brings up various C topics.

Do you not like to increment the s parameter directly?
Some people don't like to change the values of parameters.
I prefer to change them whenever it's handy.

size_t strlen(const char *s)
{
size_t n;

for (n = 0; *s != '\0'; ++s) {
++n;
}
return n;
}


Normally I prefer parameters to retain their original values and
introduce new variables instead. I'm willing to break the rule
but in the absence of a compelling reason I usually don't.

In this case though it doesn't have to come up. Sometimes the
most straightforward code is best:

size_t
strlen( const char *s ){
size_t n=0;

while( s[n] ) n++;
return n;
}

This implementation ran faster in my tests than any of the
pointer versions posted.
Nov 14 '05 #56
Tim Rentsch wrote:

<snip - portable code to implement strlen>
In this case though it doesn't have to come up. Sometimes the
most straightforward code is best:

size_t
strlen( const char *s ){
size_t n=0;

while( s[n] ) n++;
return n;
}

This implementation ran faster in my tests than any of the
pointer versions posted.


Personally I would probably do the following, since there is
initialisation, condition and increment. Purely as a matter of taste,
not correctness.

size_t strlen( const char *s )
{
size_t n;
for (n=0; s[n]; n++)
continue;
return n;
}

--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #57
El Sat, 30 Apr 2005 17:03:47 +0100, Flash Gordon escribió:

size_t strlen( const char *s )
{
size_t n;
for (n=0; s[n]; n++)
continue;
return n;
}


I wonder if that 'continue' is required. Wouldn't a simple null
statement ';' do the work?

for (n=0; s[n]; n++)
; /* continue */
Greetings.
--
Luis Alberto Giménez
JabberID: Si*******@bulma lug.net
GnuPG ID: 0x3BAABDE1
Nov 14 '05 #58
Alberto Giménez <al****@telelin e.es> writes:
El Sat, 30 Apr 2005 17:03:47 +0100, Flash Gordon escribió:
size_t strlen( const char *s )
{
size_t n;
for (n=0; s[n]; n++)
continue;
return n;
}


I wonder if that 'continue' is required. Wouldn't a simple null
statement ';' do the work?

for (n=0; s[n]; n++)
; /* continue */


Yes, in this context, an empty statement ";" is exactly equivalent to
"continue;" . The "continue" statement is just more explicit; a lone
";" is easy to miss.

I note that you felt it necessary to add a comment; as long as you're
doing that, why not use the "continue" keyword?

I would probably have written it as:

for (n=0; s[n]; n++) {
;
}

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #59
Flash Gordon <sp**@flash-gordon.me.uk> writes:
Tim Rentsch wrote:

<snip - portable code to implement strlen>
In this case though it doesn't have to come up. Sometimes the
most straightforward code is best:

size_t
strlen( const char *s ){
size_t n=0;

while( s[n] ) n++;
return n;
}

This implementation ran faster in my tests than any of the
pointer versions posted.


Personally I would probably do the following, since there is
initialisation, condition and increment. Purely as a matter of taste,
not correctness.

size_t strlen( const char *s )
{
size_t n;
for (n=0; s[n]; n++)
continue;
return n;
}


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

n = 0;
while( s[n] ) n++;

clearly establishes the postcondition

s[n] == 0 && s[k] != 0 for 0 <= k < n

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.
Nov 14 '05 #60

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
11729
by: Matt Parkins | last post by:
Hi, (I realise this probably isn't precisely the right group for this - could someone direct me to the appropriate group to post this question? - thanks !) I'm using Visual C++ 2005 Express Edition Beta (free download from MS - hooray!), and everything works fine, except I get warnings back on the use of some functions, strlen() for example, saying that the function has been deprecated - although they do still work (which is I guess...
81
7354
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
33
2977
by: apropo | last post by:
what is wrong with this code? someone told me there is a BAD practice with that strlen in the for loop, but i don't get it exactly. Could anyone explain me in plain english,please? char *reverse(char *s) { int i; char *r; if(!s) return NULL;//ERROR r=calloc(strlen(s)+1,sizeof(char));
53
717
by: ¬a\\/b | last post by:
strlen is wrong because can not report if there is some error e.g. char *a; and "a" point to an array of size=size_t max that has no 0 in it
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.