Thread: pre/post increment/decrement

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    13

    pre/post increment/decrement

    Hi all guys, was browsing Linux kernel source and I came across this version of strstr:

    Code:
    char *strstr(const char *s1, const char *s2){
    	size_t l1, l2;
    
    
    	l2 = strlen(s2);
    	if (!l2)
    		return (char *)s1;
    	l1 = strlen(s1);
    	while (l1 >= l2) {
    		l1--;
    		if (!memcmp(s1, s2, l2))
    			return (char *)s1;
    		s1++;
    	}
    	return NULL;
    }
    in the
    Code:
    while
    loop I was wondering if was possible to do instead something like:

    Code:
    while(l1-- >= l2)
    and remove the line below the while expression..

    So, it worked but is this correct? or are there any unexpected behaviour in doing a post-decrement inside a while?

    thanks in advance

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Only if you want to change the behaviour of the function so it doesn't do what is intended.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by riemann View Post

    So, it worked but is this correct? or are there any unexpected behaviour in doing a post-decrement inside a while?
    Just that original version is easier to read - so when somebody will need to modify it - less probability he will make a mistake due to misinterpreted code.
    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

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    I didn't fully get your answers :/ despite it's less readable...does it present any issues releated with the post/pre increment/decrement operators? (like using them in a printf or things like that when there could be unexpected behaviours)

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by riemann View Post
    does it present any issues releated with the post/pre increment/decrement operators?
    Since l1 is only used for the compare in while, it doesn't matter where the post decrement occurs. This should work also:

    Code:
    /* ... */
        for(l1 = strlen(s1); l1-- >= l2; s1++)
            if(!memcmp(s1, s2, l2))
                return (char *)s1;
    or

    Code:
    /* ... */
        for(l1 = strlen(s1); l1 >= l2; l1--, s1++)
            if(!memcmp(s1, s2, l2))
                return (char *)s1;

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Quote Originally Posted by rcgldr View Post
    Since l1 is only used for the compare in while, it doesn't matter where the post decrement occurs. This should work also:

    Code:
    /* ... */
        for(l1 = strlen(s1); l1-- >= l2; s1++)
            if(!memcmp(s1, s2, l2))
                return (char *)s1;
    or

    Code:
    /* ... */
        for(l1 = strlen(s1); l1 >= l2; l1--, s1++)
            if(!memcmp(s1, s2, l2))
                return (char *)s1;
    Great, gotcha now.. Thanks a lot!

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes, there is a difference in their behavior. They both perform the same number of iterations, but the value of l1 when the loop terminates will differ.
    Code:
    $ cat while.c
    #include <stdio.h>
    
    int main(void)
    {
      int l1, l2, iter;
    
    
      l1 = 5;
      l2 = 2;
      iter = 0;
      printf("Starting decrement in loop body with l1 = %d, l2 = %d\n", l1, l2);
      while (l1 >= l2) {
        l1--;
        printf("iteration %d\n", ++iter);
      }
      printf("Finishing with l1 = %d, l2 = %d\n", l1, l2);
    
    
      l1 = 5;
      l2 = 2;
      iter = 0;
      printf("Starting decrement in loop condition with l1 = %d, l2 = %d\n", l1, l2);
      while (l1-- >= l2) {
        printf("iteration %d\n", ++iter);
      }
      printf("Finishing with l1 = %d, l2 = %d\n", l1, l2);
    
    
      return 0;
    
    }
    $ make while
    gcc -Wall -ggdb3 -std=c99 -O0 -o while while.c -lm -lpthread -lrt
    $ ./while
    Starting decrement in loop body with l1 = 5, l2 = 2
    iteration 1
    iteration 2
    iteration 3
    iteration 4
    Finishing with l1 = 1, l2 = 2
    
    Starting decrement in loop condition with l1 = 5, l2 = 2
    iteration 1
    iteration 2
    iteration 3
    iteration 4
    Finishing with l1 = 0, l2 = 2
    With the decrement inside the loop body, l1 only decrements when the loop condition is true, meaning it doesn't decrement when l1 becomes small enough to cause the loop to stop. With the decrement in the loop condition, however, l1 decrements every time the loop condition is checked, which is every time the condition is true and when the condition is checked and fails. Thus, putting l1-- in the loop condition will leave l1 with a value 1 less than it would if the decrement is in the loop body.

    Now, for that implementation of strstr that you referenced, it doesn't really matter, since the value of l1 is not used after the loop. But in general, such loops are not equivalent.

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by rcgldr View Post
    Since l1 is only used for the compare in while, it doesn't matter where the post decrement occurs.
    Quote Originally Posted by anduril462 View Post
    Yes, there is a difference in their behavior. They both perform the same number of iterations, but the value of l1 when the loop terminates will differ.

    Now, for that implementation of strstr that you referenced, it doesn't really matter, since the value of l1 is not used after the loop. But in general, such loops are not equivalent.
    True. I didn't make it clear that if the value of l1 was being used after the loop exits that it would make a difference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. increment and decrement operators in c
    By Abhishek Thakur in forum C Programming
    Replies: 7
    Last Post: 07-05-2012, 04:25 AM
  2. How would I gradually decrement and increment?
    By 777funk in forum C Programming
    Replies: 15
    Last Post: 11-09-2010, 03:08 PM
  3. Increment and Decrement
    By Johnathan1707 in forum C Programming
    Replies: 4
    Last Post: 07-25-2009, 02:20 PM
  4. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 09:24 PM
  5. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 06:54 AM