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 7786
There has to be a null terminator somewhere.
Here's a short implementation:
#include <string.h>
size_t (strlen)(char *s)
{
char *p = s;
while (*p != '\0')
p++;
return (size_t)(p - s);
}
/* Gregory Pietsch */
Gregory Pietsch wrote: There has to be a null terminator somewhere.
Here's a short implementation:
#include <string.h> size_t (strlen)(char *s) { char *p = s;
while (*p != '\0') p++; return (size_t)(p - s); }
/* Gregory Pietsch */
Interesting seeing \0 so widely in use. On most systems, NULL is defined
as \0, however there are a few special cases where it is not. Shouldn't
we be using NULL instead of \0?
Joe Estock
Joe Estock wrote: Gregory Pietsch wrote:
There has to be a null terminator somewhere.
Here's a short implementation:
#include <string.h> size_t (strlen)(char *s) { char *p = s;
while (*p != '\0') p++; return (size_t)(p - s); }
/* Gregory Pietsch */ Interesting seeing \0 so widely in use. On most systems, NULL is defined as \0, however there are a few special cases where it is not. Shouldn't we be using NULL instead of \0?
Joe Estock
No Joe, NULL is the 'null pointer constant' while '\0' is a constant
character (with int type) and value zero. This is often called the null
character or the NUL character. Never NULL character.
--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Chris Torek wrote: In article <11************ *********@g14g2 000cwa.googlegr oups.com> roy <ro*****@hotmai l.com> wrote:I was wondering how strlen is implemented. What if the input string doesn't have a null terminator, namely the '\0'?
Q: What if a tree growing in a forest is made of plastic? A: Then it is not a tree, or at least, it is not growing.
If something someone else is calling a "string" does not have the '\0' terminator, it is not a string, or at least, not a C string. In C, the word "string" means "data structure consisting of zero or more characters, followed by a '\0' terminator". No terminator, no string.
Since strlen() requires a string, it may assume it gets one.
There are functions that work on "non-stringy arrays"; in particular, the mem* functions -- memcpy(), memmove(), memcmp(), memset(), memchr() -- but they take more than one argument. If you have an array that always contains exactly 40 characters, and it is possible that none of them is '\0' but you want to find out whether there is a '\0' in those 40 characters, you can use memchr():
char *p = memchr(my_array , '\0', 40);
memchr() stops when it finds the first '\0' or has used up the count, whichever occurs first. (It then returns a pointer to the found character, or NULL if the count ran out.) The strlen() function has an effect much like memchr() with an "infinite" count, except that because the count is "infinite", it "always" finds the '\0':
size_t much_like_strle n(const char *p) { const char *q = memchr(p, '\0', INFINITY); return q - p; }
except of course C does not really have a way to express "infinity" here. (You can approximate it with (size_t)-1, though.)
Pardon me Chris, but I really don't get the drift of what you are
trying to convey. These strings are also "stringy", I don't see how
these are "non-stringy".
IOW you are assuming that these "non-stringy" arrays are also supposed
to end with a null character. "Stringy" I say.
--
Imanpreet Singh Arora
>Chris Torek wrote: There are functions that work on "non-stringy arrays"; in particular, the mem* functions ... If you have an array that always contains exactly 40 characters, and it is possible that none of them is '\0' but you want to find out whether there is a '\0' in those 40 characters, you can use memchr() ...
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
Minti <im*******@gmai l.com> wrote:Pardon me Chris, but I really don't get the drift of what you are trying to convey. These strings are also "stringy", I don't see how these are "non-stringy".
If there is no '\0' byte in all 40 characters, it is not a string.
If there is a '\0' byte somewhere within those 40 characters, it
*is* a string -- and any characters after the first such '\0' are
not part of the string (but remain part of the array).
IOW you are assuming that these "non-stringy" arrays are also supposed to end with a null character. "Stringy" I say.
In other words, I am saying that these arrays do not contain strings
if and only if they do not contain a '\0'. Note that strncpy()
sometimes makes such arrays (which is one reason some people invented
strlcpy()).
If I may draw an analogy: in mathematics, a statement is false if
there is even a single counterexample. Hence "x * (1/x) = 1" is
a false statement mathematically, because it does not hold for x=0.
(But note that if we limit it, "x * (1/x) = 1 provided x \ne 0",
the statement becomes true for x \elem real, while it remains false
for x \elem integer, and so on.) (Note that details like "x is a
real number" also matter in computing, where float and double do
not really give us "real numbers", but rather approximations. )
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
"Gregory Pietsch" <GK**@flash.net > writes: There has to be a null terminator somewhere.
To clarify: This doesn't mean that there's a guarantee that there will
be a null terminator somewhere. It means that if there isn't a null
terminator anyway, you must not call strlen(). The burden is on the
caller.
(I briefly read your statement the other way.)
--
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.
On 22 Apr 2005 20:59:49 -0700, in comp.lang.c , "roy"
<ro*****@hotmai l.com> wrote: Thanks. Maybe my question should be "what if the input is a char array without a null terminator".
your question was already answered. However, a quote from hte ISO
Standard may help:
7.21.6.3 The strlen function
3. The strlen function returns the number of characters that precede
the terminating null character.
Clearly if there's no terminating null, this function can't return
anything meaningful. It may in fact not return at all, and its not
uncommon for it to return absurd numbers such as 5678905 or -456
But from my experimental results, it seems that strlen can still return the number of characters of a char array.
How can it do that? Its /required/ to search for the terminating null.
Your compiler is either not standard compilant, or its exhibiting
random behaviour.
I am just not sure whether I am just lucky or sth else happened inside strlen.
lucky
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mark McIntyre <ma**********@s pamcop.net> writes: On 22 Apr 2005 20:59:49 -0700, in comp.lang.c , "roy" <ro*****@hotmai l.com> wrote:
[...] But from my experimental results, it seems that strlen can still return the number of characters of a char array.
How can it do that? Its /required/ to search for the terminating null. Your compiler is either not standard compilant, or its exhibiting random behaviour.
strlen() is almost certainly finding a zero byte immediately after his
array. I'd expect that to be a very common manifestation of the
undefined behavior in this case. I am just not sure whether I am just lucky or sth else happened inside strlen.
lucky
No, if he'd been lucky it would have crashed the program (with a
meaningful diagnostic) rather than quietly returning a meaningless
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.
roy wrote: 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
I found some C functions coded in assembler for the 8086 way back when.
;
; -------------------------------------------------------
; int strlen(s)
; char *s;
; Purpose: Returns the length of the string, not
; including the NULL character
; -------------------------------------------------------
;
ifndef pca
include macro2.asm
include libdef.asm
endif
;
idt strlen
def strlen
strlen: qenter bx,di
mov di,parm1[bx]
; cmp di,zero
; jz null
mov ax,ds
mov es,ax
mov cx,-1
xor al,al
cld
repnz scasb
not cx
dec cx
mov ax,cx
exitf
;null xor ax,ax
; exitf
modend strlen
I guess it's C equivelent is:
unsigned
strlen( char *string )
{
unsigned rv = -1;
while( *string ) rv--, *string++;
rv = (-rv) - 1;
return rv;
}
of course I'd just write it like this:
size_t
strlen( char *string )
{
size_t rv = 0;
while ( *string++ ) rv++;
return rv;
}
Keith Thompson wrote: Mark McIntyre <ma**********@s pamcop.net> writes:
On 22 Apr 2005 20:59:49 -0700, in comp.lang.c , "roy" <ro*****@hotm ail.com> wrote:
[...]
But from my experimental results, it seems that strlen can still return the number of characters of a char array.
How can it do that? Its /required/ to search for the terminating null. Your compiler is either not standard compilant, or its exhibiting random behaviour.
strlen() is almost certainly finding a zero byte immediately after his array. I'd expect that to be a very common manifestation of the undefined behavior in this case.
I am just not sure whether I am just lucky or sth else happened inside strlen.
lucky
No, if he'd been lucky it would have crashed the program (with a meaningful diagnostic) rather than quietly returning a meaningless result.
So, you are saying this is a poorly implemented compiler? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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));
|
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
|
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...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |