Jump to content
  • Advertisement
Sign in to follow this  
nivlekio

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
size_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 this post


Link to post
Share on other sites
Quote:
Original post by staticVoid2
Quote:
Original post by ToohrVyk
size_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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!