Quote: while(1); { cout << "In while Loop" << endl if(argument) { code........ } else { break; } }As you can see there is a semi colon after the while command. Some how this made the program loop forever at the while(1) bit and not execute the code in the braces. I know this becuase it did not print out the "In while Loop" string. But after i noticed this mistake ( 2hrs later :( ) i took off the semi colon and the program entered the while loop braces and executed the code. then it broke out of it then the if statement was false. Seeing i did not get an error from the compiler why would some one want to do this if its proper c++ code?
C++ Bug????
I was working on networking and i made a silly mistake.
Quote:Some how this made the program loop forever at the while(1) bit and not execute the code in the braces. I know this becuase it did not print out the "In while Loop" string.
Of course, because while(1); is an infinity loop.
Quote:Seeing i did not get an error from the compiler why would some one want to do this if its proper c++ code?
Because C and C++ do not have any strict rules on how you format your code. ; is used to end a statement (in your case, while(1)), which has caused the infinity loop.
Because there is no strict formatting rules, it allows us to write a given statement different ways.
Also, because { and } are used for creating a new local scope, there is no errors in your code--with or without the semicolen (;).
Newer versions of GCC warn on this kind of construct as it's very dangerous and easily overlooked. Have found this bug in somebody elses javascript as well.<br><br>GCC now requires you (if you enable the warnings) to write<br><br>while(1) {}<br><br>as it will refuse the semicolon for such use.
I've often seen such statements coded as:
It makes it look more like an if statement with one following line without braces. Just a blank line in this case.
Although a lot of people aren't so fond of using whiles or ifs without any braces. Which makes sense. Not using braces has hurt me before. (Well I guess I hurt myself because I wasn't paying attention).
while(statement_is_true) ;
It makes it look more like an if statement with one following line without braces. Just a blank line in this case.
Although a lot of people aren't so fond of using whiles or ifs without any braces. Which makes sense. Not using braces has hurt me before. (Well I guess I hurt myself because I wasn't paying attention).
Quote:Original post by ToohrVyksize_t strlen(const char *s){ size_t i = 0; while (s[i++] != '\0'); return i - 1;}
size_t strlen(const char *s){ size_t i = 0; while (s != 0) i++; return i;}
faster?
Quote:Original post by staticVoid2It'll generate the exact same assembly, so no.Quote:Original post by ToohrVyksize_t strlen(const char *s){ size_t i = 0; while (s[i++] != '\0'); return i - 1;}
*** Source Snippet Removed ***
faster?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement