A line of code with logic errors but no syntax errors

Source: Internet
Author: User

Using C ++ to call windows APIs for non-blocking TCP communication, one line of code is written as follows:

If (socket_error = (sock, (sockaddr *) & ADDR, sizeof (ADDR ))) <br/>{< br/> cout <"bind failed" <Endl; <br/> wsacleanup (); <br/> return 0; <br/>}

This code compilation has no errors or warnings, but the results are not what I want. After debugging for half a day, there was no result. Finally, there is no way. I copied the code to codeblocks and compiled it with GCC. The result is the same. When I checked the code again, I found that the code bound to the local IP address and port was very strange. I wrote the BIND (...) (...). = The right side of the operator is a comma expression, which returns sizeof (ADDR), which is just an integer, so there is no syntax error. The correct code should be as follows:

If (socket_error = BIND (sock, (sockaddr *) & ADDR, sizeof (ADDR ))) <br/>{< br/> cout <"bind failed" <Endl; <br/> wsacleanup (); <br/> return 0; <br/>}

I think when I write code, I should not put BIND (...) written (...), it may be that during the encoding process, we accidentally gave "bind" to Ctrl + X. However, such errors are hard to be found.

 

If I use C # To write similar code, I believe I will not make such a mistake. Because in. in the. NET platform, the enumerated colors displayed in the editor are different from the general methods and attributes. net objects are self-described, and the powerful smart prompting function reduces the probability of making similar errors. Indeed, a good tool can improve coding efficiency and reduce the chance of making mistakes. Think about it. If. Net does not have any supporting tools such as Visual Studio, it is possible that. NET will not become so popular today.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.