473,765 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: D.E. Knuth's strlen

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

#include <stdio.h>
#include <string.h>
#define H 0x8080808080808 080ULL
#define L 0x0101010101010 101ULL
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_Keit h) 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 3675
Keith Thompson wrote:
jacob navia <ja***@nospam.c omwrites:
[...]
>Knuth's strlen then, looks like this.

#include <stdio.h>
#include <string.h>
#define H 0x8080808080808 080ULL
#define L 0x0101010101010 101ULL

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******@hotma il.comwrites:
Keith Thompson wrote:
>jacob navia <ja***@nospam.c omwrites:
[...]
>>Knuth's strlen then, looks like this.

#include <stdio.h>
#include <string.h>
#define H 0x8080808080808 080ULL
#define L 0x0101010101010 101ULL

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_Keit h) 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
8488
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 your instruction.
81
7342
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
2970
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));
66
7785
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
15609
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 <stdio.h> #include <string.h> void main() { char *str1="abcd";
2
2264
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 headache. #include <stdio.h> #include <string.h>
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
31
470
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 a page boundary with the next page not mapped in memory. And some machine allows unaligned access but at a performance penality, which would be against the goal of using that trick.
0
10007
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
9951
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
8831
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
7375
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
6649
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
5275
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...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
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
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.