C++ Bug????

This topic is 3786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I was working on networking and i made a silly mistake.
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?

Share on other sites
size_t strlen(const char *s){  size_t i = 0;  while (s[i++] != '\0');  return i - 1;}

Share on other sites
ah kk mmm did not know you could do that thanks

Share on other sites
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 (;).

Share on other sites
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.

GCC now requires you (if you enable the warnings) to write

while(1) {}

as it will refuse the semicolon for such use.

Share on other sites
I've often seen such statements coded as:

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).

Share on other sites
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?

Share on other sites
void strcpy(char *d, const char *s){  while (*d++ = *s++);}

Share on other sites
Quote:
Original post by staticVoid2
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?
It'll generate the exact same assembly, so no.

Share on other sites
just a thought, because it had one less loop and one less subtract.

1. 1
Rutin
19
2. 2
JoeJ
15
3. 3
4. 4
5. 5

• 24
• 20
• 13
• 13
• 17
• Forum Statistics

• Total Topics
631699
• Total Posts
3001776
×