Thread: Playing around strlen :)

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Wink Playing around strlen :)

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /*
    
    The std's strlen algorithm:
    From: Programming with lcc-win32 by Jacob Navia and Q Software Solutions GmbH
    
    int strlen(char *str)
    {
       char *p = str;
    
       while(*p != 0)
          p++;
    
       return p - str;
    }
    
    It will crash in both..
    
       char *test;
       printf("%d", strlen(test));
    
    ..and..
    
       char *test = NULL;
       printf("%d", strlen(test));
    
    */
    
    //This is my modified std's strlen:
    
    int strlen_mod(char *str)
    {
       //Avoiding crashes by initializing string to NULL
       if(str == NULL) //a very simple fix
          return 0;
    
       char *p = str;
    
       while(*p != 0)
          p++;
    
       return p - str;
    }
    
    
    //They said passing by reference gives better performance:
    
    int strlen_byref(char **str)
    {
       if(*str == NULL)
          return 0;
    
       char *p = *str;
    
       while(*p != 0)
          p++;
    
       return p - *str;
    }
    
    
    //But, let's try something simple :) :
    
    int strlen_simple(char **str)
    {
       if(*str == NULL)
          return 0;
    
       int len = 0;
    
       while((*str)[len] != 0)
          len++;
    
       return len;
    }
    
    int main()
    {
       char *str = "abcde";
    
       printf("%s\n\n", str);
    
       printf("strlen       : %d\n", strlen(str));
       printf("strlen_mod   : %d\n", strlen_mod(str));
       printf("strlen_byref : %d\n", strlen_byref(&str));
       printf("strlen_simple: %d\n", strlen_simple(&str));
    
       return 0;
    }
    Comments are welcome

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Well I think strlen was written that way because it expects the string to have something and not simply just be the declared variable passed in. But you are right in doing the NULL check.
    =========================================
    Everytime you segfault, you murder some part of the world

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    However, I don't believe passing a pointer to pointer buys you anything. It just adds indirection, which should make everything you do with the pointer slower.

    Passing a pointer instead of a value gives better performance if the object is large (e.g a struct with many fields). A char* is a small thing (the size of a pointer, i.e (most probably) exactly the same size as char**).
    Last edited by anon; 06-13-2008 at 12:44 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I believe strlen() was written without a NULL check to make it faster, since most experienced programmers will do their own NULL checking (if there's a chance of it being NULL).
    As for preventing the first crash, there's no portable way to check if a pointer has been initialized. You just have to assume the programmer knows better than that.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Code:
    //They said passing by reference gives better performance:
    
    int strlen_byref(char **str)
    That's not passing by reference. You cannot pass by reference in C. That's passing a pointer to a pointer, by value, and it only decreases performance.

    You can't stop a crash from occurring when passing in an uninitialised pointer. Checking for NULL is the caller's responsibility, as usually the passed pointer is to a buffer and would not be NULL.

    Comments are welcome
    Questions are welcome
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >int strlen(char *str)
    The library strlen() returns a size_t versus an int, which would allow for longer strings. I like strlen_simple(), but with a char *str instead of char **str as the parameter. It requires one local variable versus the first version, but I don't see where that makes it less attractive than the first version.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The standard C strlen() also takes a const char *, because strlen() doesn't need to modify the string that it is passed.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PC or Console gamers, what are you playing?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 119
    Last Post: 04-14-2008, 08:17 AM
  2. How do I keep the sounds playing?
    By Queatrix in forum Windows Programming
    Replies: 0
    Last Post: 07-21-2007, 10:19 AM
  3. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 10:28 AM
  4. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  5. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM

Tags for this Thread