Thread: To recreate strcpy

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and if you walk with the unsigned char pointer - your zero will be always less than something else
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by mike_g View Post
    From what it says in my C++ book the ! operator has higher precedence than: ||, >, and <. From that I presumed that the if statement sequence would run the 'not' check first, but maybe I was wrong about that.
    You're right that it has higher precedence, but that only means that:
    Code:
    if (!a || b)
    is equal to
    Code:
    if ((!a) || b)
    instead of
    Code:
    if (!(a || b))
    In your case there can be no confusion as to the meaning of the statement with || and !, because there is only one identifier following the !, so the ! can therefore only apply to that identifier.

    Precedence is only about the meaning of the statement, i.e. what the syntax tree actually looks like. It is not about what the order of evaluation of nodes within the syntax tree is.
    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"

  3. #18
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah I kind of came to realise that the 'not' check was redundant, but dident say anything cos I'd look dumb :P I guess theres no sneaking my dodgy code past you guys tho

    Anyway my first attempt sucked so I had a go at rewriting the function. This should be faster, and I managed to shave a few characters off it:
    Code:
    int StrCmp(char *a, char *b) 
    {   
        while(*a || *b) 
        { 
            if(*a != *b) return(*a > *b) ? 1 : -1; 
            *a++; *b++;
        }                 
        return 0;         
    }

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Code:
      *a++; *b++;
    You can get rid of the stars since you're not using the values of *a and *b anyway.

  5. #20
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mike_g View Post
    Code:
    int StrCmp(char *a, char *b) 
    {   
        while(*a || *b) 
        { 
            if(*a != *b) return(*a > *b) ? 1 : -1; 
            *a++; *b++;
        }                 
        return 0;         
    }
    This still compares against the null byte. Try writing it using while(*a && *b) instead (other stuff will change around too).

  6. #21
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by robatino
    You can get rid of the stars since you're not using the values of *a and *b anyway.
    Nice one. Now when I remove the whitespace it fits snuggly into one line of code, which fits with the rest of the functions

    Quote Originally Posted by brewbuck
    This still compares against the null byte.
    I see I forgot to make the char pointer unsigned which would cause problems, but apart from that wouldent the null byte equate to less than whatever it is being compared against anyway?

  7. #22
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mike_g View Post
    Nice one. Now when I remove the whitespace it fits snuggly into one line of code, which fits with the rest of the functions


    I see I forgot to make the char pointer unsigned which would cause problems, but apart from that wouldent the null byte equate to less than whatever it is being compared against anyway?
    In this case, yes.

  8. #23
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    I wrote my own fmod function that only dealt with cases where the number was less than twice that of the number you want to divide by.

    It was significantly faster.

    I think that's what put me off relying on other people's code.

  9. #24
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I wrote several functions in a basic language that were apparently faster than the equivalent functions that were built in. I think the difference was mainly down to function overheads (or something like that) rather than my code actually being faster.

  10. #25
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

    Question

    Quote Originally Posted by samGwilliam View Post
    I wrote my own fmod function that only dealt with cases where the number was less than twice that of the number you want to divide by.

    It was significantly faster.

    I think that's what put me off relying on other people's code.
    Even the standard library?

  11. #26
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by samGwilliam View Post
    I wrote my own fmod function that only dealt with cases where the number was less than twice that of the number you want to divide by.

    It was significantly faster.

    I think that's what put me off relying on other people's code.
    I assume you turned on compiler optimizations when you did the testing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. Where is strcpy() defined? (Can't find in string.h ect)
    By Zero_Point in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2006, 05:14 PM
  4. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 11:00 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM