Experience Sharing: Four debugging tips in C ++ Programming

Source: Internet
Author: User

The following describesC ++The four tips for programming are for your reference.

1. debugging mark

Applicable to preprocessing # define to define one or more debugging tags. In the code, use # ifdef and # endif for debugging management. After the program is finally debugged, you only need to use the # undef flag to debug the code. The commonly used debugging mark is DEBUG, and the statement sequence is as follows:

# Define DEBUG

# Ifdef DEBUG

Debug code

# Endif

2. debugging mark during running

Enable and disable the debugging flag during the running of the program. You can set a debug bool flag. This is more convenient for the program running on the command line.

For example, the following code

 
 
  1. # Include <iostream>
  2. # Include <string>
  3. Using namespace std;
  4. Bool debug = false;
  5. Int main (int argc, char * argv [])
  6. {
  7. For (int I = 0; I <argc; I ++)
  8. If (string (argv [I]) = "-- debug = on")
  9. Debug = true;
  10. Bool go = true;
  11. While (go)
  12. {
  13. If (debug)
  14. {
  15. Debug code
  16. } Else {}
  17. }
  18. }

3. Convert variables and expressions into strings

However, the string operator is used to define the conversion output.

 
 
  1. #define PR(x) cout<<#x”=”<<x<<'\n' 

4. C language assert ()

In <assert>, when assert is used, a parameter is provided, that is, a true expression. The Preprocessor generates code to test the assertions. If the assertions are not true, an error message is sent to indicate what the assertions are and if they fail for a while, the program will terminate.

 
 
  1. # Include <assert>
  2. Using namsapce std;
  3. Int main ()
  4. {
  5. Int I = 100;
  6. Assert (I! = 100); // Fails
  7. }
  8. After debugging, add # define NDEBUG before # include <assert> to eliminate the code generated in red.
  9. }

I hope this article will help you. Let's take a look.

Related Article

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.