Methods and tools for reducing memory defects in C + + coding

Source: Internet
Author: User
Tags coding standards constructor resource visual studio

C + + language is the mainstream development language of desktop system, especially system software and large application software. The C + + language is known for its flexibility and is more complex. Using C + + to write robust code is more challenging. C + + allows dynamic memory management while also easily leading to more memory-related problems. In general, in addition to system design defects, C + + software defects and errors are mostly related to memory defects, mainly including memory access errors and memory leaks two types. Therefore, eliminates the memory related flaw in the code, becomes the programmer to write, the debugging, the Maintenance Code task, is also guarantees the software quality the key.

The work of this paper is based on "863" project "Large-scale GIS for network mass spatial information". The system is written based on C++/MFC, and the development environment is visual Studio. NET 2003. Based on the project practice, this paper summarizes how to use C + + language mechanism, development environment and related quality assurance tools to prevent and discover various compile-time, run-time and memory-related defects of methods and tools.

1 Follow C + + coding specifications and idioms to prevent defects

Code specification is a language-related rule, which is the experience summed up by practice. Good programming standards will effectively help developers avoid developing potentially dangerous code. In general, to reduce memory defects, the following coding rules should be followed [1]:

(1) A base class or a class with a virtual function should declare its destructor as a virtual function.

(2) Prevent memory leaks in the constructor, and do not throw exceptions in the destructor.

(3) using the corresponding form of new and delete. That is: Use Delete to release the memory requested by new, delete[] to release the memory requested by new[.

(4) The pointer must be initialized before it is used, and the pointer to dynamic memory should be immediately empty after it is released.

(5) If a resource is allocated in a class constructor, you need to explicitly provide the copy constructor and assignment operator, and release the resource in the destructor.

What is worth paying attention to is the idiomatic method Raii in C + +. Raii's core idea is to use objects to manage resources, to acquire resources in the object's constructors, and to release resources in its destructor [2]. In order to ensure that the dynamically requested memory can be released even if there is an exception, the ideal approach is to use local variables to manage the ownership of dynamic memory (ownership), the so-called smart pointer. The AUTO_PTR in STL is designed to solve the problem of resource ownership, but it lacks support for reference numbers and arrays and cannot be used in STL containers. Boost Library [3] provides intelligent pointers that are relatively mature and of high practical value. Where the shared_ptr thread is safe and can be used in an STL container. Specific example reference [3].

1.1 Code Specification Inspection tool Codewizard

Codewizard can automatically scan, analyze and check the source program directly. When an offence is found, the information is communicated to which rule does not conform and an explanation is given. In the case of Codewizard 4.3, there are more than 500 coding standards built into it. Codewizard can choose which coding standards to execute for the current project. Codewizard can be tightly integrated with VC + +, after installation, VC + + Codewizard tool bar.

1.2 Code Check Tool Pc-lint

Pc-lint can check for errors that are not easily discovered by the compiler. Pc-lint can examine more than 100 C library functions and discover more than 1 000 common errors in standard C + + code. To integrate Pc-lint with Visual Studio, you need to configure yourself. Jon Zyzyck provides a report Builder that can help you do this work. can be downloaded at http://www.ddj.com. The literature [4] shows how to integrate Pc-lint in VC + + environment.

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.