infinite loop error

This is a discussion on infinite loop error within the C Programming forums, part of the General Programming Boards category; why am i getting an infinite loop in this piece of code: Code: while((( *(str+index)) != '(') || ((*(str+index)) ! ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    19

    infinite loop error

    why am i getting an infinite loop in this piece of code:

    Code:
                 while((( *(str+index)) != '(') || ((*(str+index)) ! = '\0'))//while char is not '(' and not end of string, increase index 
                 {
                      index++;
                 }
    Is it because i should have flushed something and i didn't??

    If that's the case how do i do that??

    coz i've been trying delete [] str;

    and this doesn't work

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It can't be both '(' and '\0' at the same time. Thus, it should be "while it isn't this AND it isn't that".

    Also, delete is a C++ keyword.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    12
    try
    Code:
        if( index < strlen(str) && index >= 0) /*be sure...*/
        while( str[index] != '\0' && str[index++] != '('  );
    Last edited by Salem; 05-24-2006 at 12:56 AM. Reason: Fix code tags - its / not \

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > while( str[index] != '\0' && str[index++] != '(' );
    Would be better written as
    while( str[index] != '\0' && str[index] != '(' ) index++;

    Remember, && and || are short-circuited, so if the right hand side is skipped, then the side effect of the ++ DOES NOT HAPPEN!
    Needless to say, this is a bizarre bug to find.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    17
    you even put in your comment

    //while char is not '(' and not end of string, increase index

    make it && and it should work.

    Also with this line:

    while( str[index] != '\0' && str[index++] != '(' );

    remember that i++ is postfix and depending on the order that you want things to happen and if you want index to increase, that statement is undefined.
    It is undefined because the while could evaluate from the right, then index would be increased for str[index] but not for str[index++]. But since it is postfix, if you want it to increase and it evaluates from the left it won't be increased for the evaluation. Then also what the guy before said, if the left hand side is false the right hand side won't even be evaluated (that is of course if it evaluates from left to right). So I wouldn't be doing that since you don't know what order things are all going to occur in.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > It is undefined because the while could evaluate from the right,
    No - the order in which && and || are evaluated is very specifically defined.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    This while loop, while it works, seems overly complex & is causing headaches. Why not just search for a '(' using strchr() ? For example, any reason why this wouldn't work:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char *test = "Test! =) Good day.";
    	int index = 0;
    	char *cfind;
    
    	cfind = strchr(test, ')');
    	if(cfind) index = cfind - test;
    	else index = strlen(test);
    
    	printf("Index is: %d\n", index);
    
    	return 0;
    }
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    19
    Well it's beacause i'm converting my program from C++ to C and i didn't use any available method to find a character. Thanks to you all, i've found my mistake.

    A really silly mistake i can say. :P

    Sorry for all this trouble.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21