473,379 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Re: D.E. Knuth's strlen

jacob navia <ja***@nospam.comwrites:
[...]
Knuth's strlen then, looks like this.

#include <stdio.h>
#include <string.h>
#define H 0x8080808080808080ULL
#define L 0x0101010101010101ULL
H and L are used only within myStrlen; I'd declare them inside the
function. Yes, I know macro definitions aren't scoped that way, but
it's useful for documentation.
size_t myStrlen(char *s)
{
[snip]
}

#ifdef TEST
int main(int argc,char *argv[])
{
char *str = "The lazy fox jumped over the slow dog";

if (argc 1) {
str = argv[1];
}
printf(
"Strlen of '%s' is %d (%d)\n",
str,strlen(str),myStrlen(str));
You're using a %d format for a size_t value.

And I'd add a "return 0;" here, even though it isn't strictly
necessary in C99.
}
#endif
As I mentioned elsethread, the algorithm (which I snipped here) is not
portable due to alignment issues, and I believe there's no way to make
it 100% portable. But there are *mostly* portable tricks that can
detect pointer alignment on *most* systems (and testing can detect
systems where the tricks don't work). And, of course, an
implementation of strlen() doesn't have to be written in 100% portable
C; it can use whatever system-specific tricks it likes.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 17 '08 #1
2 3617
Keith Thompson wrote:
jacob navia <ja***@nospam.comwrites:
[...]
>Knuth's strlen then, looks like this.

#include <stdio.h>
#include <string.h>
#define H 0x8080808080808080ULL
#define L 0x0101010101010101ULL

H and L are used only within myStrlen; I'd declare them inside the
function. Yes, I know macro definitions aren't scoped that way, but
it's useful for documentation.
So make them const unsigned long long.

--
Ian Collins
Oct 17 '08 #2
Ian Collins <ia******@hotmail.comwrites:
Keith Thompson wrote:
>jacob navia <ja***@nospam.comwrites:
[...]
>>Knuth's strlen then, looks like this.

#include <stdio.h>
#include <string.h>
#define H 0x8080808080808080ULL
#define L 0x0101010101010101ULL

H and L are used only within myStrlen; I'd declare them inside the
function. Yes, I know macro definitions aren't scoped that way, but
it's useful for documentation.
So make them const unsigned long long.
Yes, of course. There are cases where a declaration of a const object
can't replace a macro definition; I should have realized this isn't
one of them.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 17 '08 #3

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

Similar topics

21
by: sugaray | last post by:
hi, it just came up my mind that since we can get the length of any given string literal S with 'sizeof S-1', so, what's the merit of library function strlen()'s existence ? thanx in advance for...
81
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...
33
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...
66
by: roy | last post by:
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
83
by: rahul8143 | last post by:
hello, what is difference between sizeof("abcd") and strlen("abcd")? why both functions gives different output when applied to same string "abcd". I tried following example for that. #include...
2
by: Chris | last post by:
Hi there, I have been reading in this group for a while really enjoy this pool of infinite c wisdom. :) Anyway, I have question now. I have this little piece of code that is giving me a...
53
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
31
by: Jean-Marc Bourguet | last post by:
jacob navia <jacob@nospam.comwrites: I'd first align and then use that. You may get a trap with unaligned access even on machine where unaligned access doesn't normally trap if you stump on...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.