VC + + Debugging methods and techniques

Source: Internet
Author: User
Tags assert

Code style for easy debugging:

No global variables

All variables are initialized, and the member variables are initialized in the constructor

Use const as much as possible

A detailed note

VC + + Compilation options:

Always use the/W4 warning level

Always use the/GZ compilation option in debug builds to discover errors in release releases

Compile without warning: Ensure no warnings after compilation, but carefully check before eliminating warnings

Debug Method:

1. Use Assert (principle: as simple as possible)

The assert only takes effect under debug and is not compiled under release.

Example:

char* strcpy(char* dest,char* source)
{
  assert(source!=0);
  assert(dest!=0);
  char* returnstring = dest;
  
  while((*dest++ = *source++)!= ‘\0’)
  {
    ;
  }
  return returnstring;
}

2. Defensive programming

Example:

char* strcpy(char* dest,char* source)
{
  if(source == 0)
  {
    assert(false);
    reutrn 0;
  }
  if(dest == 0)
  {
    assert(false);
    return 0;
  }
  char* returnstring = dest;
  while((*dest++ = *source++)!= ‘\0’)
  {
    ;
  }
  return returnstring;
}

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.