Thread: Evaluating logical AND expression

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    Evaluating logical AND expression

    Hello friends,
    I need one confirmation.
    Please look at the following code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void sort (int[], int);
    
    int main ( void )
    {
    	int i;
    	int array[10];
    
    	for (i = 0; i < 10; i++)
    	{
    		array[i] = rand() % 100;
    		printf("%d ", array[i]);
    	}
    	putchar('\n');
    	sort (array, 10);
    	for (i = 0; i < 10; i++)
    	{
    		printf("%d ", array[i]);
    	}
    
    	return 0;
    }
    
    void sort (int array[], int len)
    {
    	int i, j, save_var;
    
    	for (i = 1; i < len; i++)
    	{
    		save_var = array[i];
    		j = i - 1;
    		while (j >= 0 && array[j] > save_var)
    		{
    			array[j + 1] = array[j];
    			j --;
    		}
    		array[j + 1] = save_var;
    	}
    }
    please look at the sort function. At some point in program execution "j" will
    become -1. In that case condition in while loop is false. However, since
    in condition there is logical AND with other condition array[j] >save_var, at this
    point this will become array[-1], and of course it would cause crash of the program.
    I think that this doesn't matter because in C AND is short circuited and therefore
    since j is -1 and j >= 0 is false, other part of condition will not be evaluated
    and everything will work fine.
    Am I right here?
    Thanks
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Am I right here?
    Yes.
    My best code is written with the delete key.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Thanks Prelude, BTW when we can expect more good stuff (tutorials) from you?
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The next tutorial should be my rewrite of splay trees, but it's somewhat stalled at the moment as I'm looking for a way to distinguish it from other resources. I'm also working on a few topics focusing on assembly language (my low level fever has kicked in again), but my free time is more often than not being spent doing other things, so I can't give you a time frame.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    12

    reply

    you are right, at some point of time j becomes -ve. But after j becomes -ve,it come back to the while loop to check the condition. since j>=0 , so while loop will not run. so there is no condition when array[-ve] will occure.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >at some point of time j becomes -ve
    Okay, just for future reference, abbreviations like -ve will confuse people. If you use proper English, you'll be properly understood.
    My best code is written with the delete key.

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Yes, I saw -ve but I understand it like "minus something". Never mind. I have to say that I think it's not good practice to rely on the fact that C will short circuit AND expression. I had very rough time recently because I programmed a Siemens S7-300 PLC in SCL (language which is very similar to Pascal) that same algorithm and every time I tried to execute programm, CPU went to fault mode. It took me a while to understand that in SCL (and probably in Pascal) AND expression is not short circuited and can introduce a lot of unexpected behavior.
    I solved problem using another temp variable:
    Code:
    temp = array[j];
    /*....*/
    while (j >= 0 && temp >save_var)
    /*...*/
    And in future, I'll always try to avoid short circuit conditions.

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>abbreviations like -ve will confuse people

    Too true, I had a lecturer earlier this year who used it a lot. From
    what I gather, the notation corresponds to postive and negative
    thus giving +ve and -ve
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    And in future, I'll always try to avoid short circuit conditions.
    What problem? In this case the short-circuiting is what should be making it work without a workaround. array[j] will still be checked if j >= 0 evaluates as true.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And in future, I'll always try to avoid short circuit conditions.
    Why? Just because some other language uses full boolean evaluation doesn't mean you can't take advantage of short circuiting in C. That's just silly.
    My best code is written with the delete key.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Micko
    I have to say that I think it's not good practice to rely on the fact that C will short circuit AND expression.
    I'd say its practice is just fine in C, it's other languages that have issues.

    But to each his own. Perhaps rewrite the loop in an equivalent manner.
    Code:
          for ( j = i - 1; j >= 0; --j )
          {
             if ( array[j] <= save_var )
             {
                break;
             }
             array[j + 1] = array[j];
          }
    But then, I'm not sure about these other languages.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Hehe, that's right. It's not problem in C, but I used to use it all time, and eventually without thinking I rewrote C code in Pascal like SCL and had a lot of problems, because there was a lot of other code and CPU goes to fault indicating error with description from which I couldn't conclude what's wrong. It took me about two hours to find out what is worng. Everything is OK when "thinking in C" but things get complicated if that logic is used "mechanicaly".

    Thanks for replies.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. Logical parameter in parent constructor
    By g4j31a5 in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2006, 04:20 AM
  3. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  4. arithmetic calculation
    By wombat in forum C Programming
    Replies: 3
    Last Post: 09-02-2002, 05:17 PM
  5. logical expression question
    By JohnMayer in forum C Programming
    Replies: 1
    Last Post: 07-22-2002, 03:46 PM