C ++ programming debugging tips ---- Reading Notes

Source: Internet
Author: User

Most of the causes of Memory leakage are due to the fact that space is allocated but not released. But this will not cause errors. In general, there are many ways to solve this problem, such as the smart pointers of com and boost, which provide a solution to this problem under some circumstances. We will discuss the pointers in two cases, but we can establish a smart point of our own, and we also need to consider three situations: 1. whether to allow replication of the smart point. If yes, which one of the multiple copies in the smart point is responsible for deleting the objects they are pointing to (com is the last one)? 2. whether smart point indicates a pointer to an object or a pointer to an object array (that is, it should use the delete operator with square brackets or without square brackets) 3. whether the smart point corresponds to a constant pointer or a constant pointer. In the preceding three cases, the following two smart points can be used: 1. REFERENCE The counting pointer (also known as the sharing pointer, that is, the smart point in com) 2. the difference between the scope pointer and the smart point is that the reference counting pointer can be copied, but the scope pointer cannot be copied. However, the scope pointer is more efficient. Reference counting pointer: scpp_refcountptr.h: [cpp] # ifndef _ SCCP_SCOPEDPTR_H _ # define _ SCCP_SCOPEDPTR_H _ # include "scpp_assert.h" namespace scpp {template <typename T> class RefCountPtr {public: explicit RefCountPtr (T * p = NULL) {Create (p);} RefCountPtr (const RefCountPtr <T> & rhs) {Copy (rhs );} refCountPtr <T> & operator = (const RefCountPtr <T> & rhs) {if (ptr _! = Rhs. ptr _) {Kill (); Copy (rhs);} return * this;} RefCountPtr <T> & operator = (T * p) {if (ptr _! = P) {Kill (); Create (p) ;}return * this ;}~ RefCountPtr () {std: cout <"kill" <std: endl; Kill ();} public: T * Get () const {return ptr _;} T * operator-> () const {std: cout <"in this" <std: endl; SCPP_TEST_ASSERT (ptr _! = NULL, "Attempt to use operator-> on NULL pointer."); return ptr _;} T & operator * () const {SCPP_TEST_ASSERT (ptr _! = NULL, "Attempt to use operator-> on NULL pointer. "); return * ptr _;} private: void Create (T * p) {ptr _ = p; if (ptr _! = NULL) {refCount _ = new int; * refCount _ = 1 ;}else {refCount _ = NULL ;}} void Copy (const RefCountPtr <T> & rhs) {ptr _ = rhs. ptr _; refCount _ = rhs. refCount _; if (refCount _! = NULL) {++ (* refCount _) ;}} void Kill () {if (refCount _! = NULL) {if (-- (* refCount _) = 0) {delete ptr _; ptr _ = NULL; delete refCount _; refCount _ = NULL ;}} private: T * ptr _; int * refCount _;} // namespace scpp # endif // _ SCCP_SCOPEDPTR_H _ test code (vs2012 + win7 environment ): [cpp] // debug. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include" scpp_assert.h "# include" iostream "# include" scpp_vector.h "# include" inline "# include" scpp_matrix.h "# include" algorithm "# include" scpp_types.h "# include" scpp_refcountptr.h "# define STUDENTAGE 10 class Student {public: student (int age): age _ (age) {} void ShowAge () {std: cout <"my age is:" <age _ <std :: endl;} private: int age _;}; int _ tmain (in T argc, _ TCHAR * argv []) {scpp: RefCountPtr <Student> smartPoint (new STUDENTAGE); scpp: RefCountPtr <Student> smartPoint2; smartPoint2 = smartPoint; // @ test: Value assignment operator smartPoint = NULL; smartPoint2-> ShowAge (); scpp: RefCountPtr <Student> smartPoint3 (smartPoint2); // @ test: copy the constructor smartPoint2 = NULL; smartPoint3-> ShowAge (); scpp: RefCountPtr <Student> * smartPoint4; // @ test: Reference smartPoint4 = & smartPoi Nt3; smartPoint4-> Get ()-> ShowAge (); Student studen3 = * smartPoint3; // @ test: Get () studen3.ShowAge (); smartPoint3 = NULL; student * student4 = new STUDENTAGE); // @ usage scpp: RefCountPtr <Student> smartPoint5; smartPoint5 = student4; smartPoint5-> ShowAge (); return 0 ;} scope pointer: if you do not intend to copy a smart pointer (so the copy constructor and value assignment operator are declared as private), you only want to ensure that the allocated resources will be correctly recycled, it will reduce the space scpp_scopedptr.h: [cpp] # ifndef _ SCCP_ SC of the int * storage technology. OPEDPTR_H _ # define _ SCCP_SCOPEDPTR_H _ # include "scpp_assert.h" namespace scpp {template <typename T> class ScopedPtr {public: explicit ScopedPtr (T * p = NULL ): ptr _ (p) {} ScopedPtr <T> & operator = (T * p) {if (ptr _! = P) {delete ptr _; ptr _ = p;} return * this ;}~ ScopedPtr () {delete ptr _;} T * operator-> () const {SCPP_TEST_ASSERT (ptr _! = NULL, "Attempt to use operator-> on NULL pointer."); return ptr _;} T & operator * () const {SCPP_TEST_ASSERT (ptr _! = NULL, "Attempt to use operator-> on NULL pointer. "); return * ptr _;} T * Release () {T * p = ptr _; ptr _ = NULL; return p;} private: T * ptr _; scopedPtr (const ScopedPtr <T> & rhs); ScopedPtr <T> & operator = (const ScopedPtr <T> & rhs );};} // namespace scpp # endif // _ SCPP_SCOPEDPTR_HPP_INCLUDED _ test code (vs2012 + win7 environment): [cpp] # include "stdafx. h "# include" scpp_assert.h "# include" iostream "# include" scpp _ Vector. h "# include" scpp_array.h "# include" scpp_matrix.h "# include" algorithm "# include" scpp_types.h "# include" principal "# include" principal "# define STUDENTAGE 10 class Student {public: student (int age): age _ (age) {} void ShowAge () {std: cout <"my age is:" <age _ <std :: endl;} private: int age _ ;}; int _ tmain (int argc, _ TCHAR * argv []) {scpp: ScopedPtr <Student> sma RtPoint (new Student (STUDENTAGE); // @ test: constructor smartPoint-> ShowAge (); scpp: ScopedPtr <Student> smartPoint2; // @ test: student * student = new STUDENTAGE; // @ usage: smartPoint2 = Student; smartPoint2-> ShowAge (); // @ test: overload-> (* smartPoint2 ). showAge (); // @ test: Reload * scpp: ScopedPtr <Student> * smartPoint3; // @ test: Release () smartPoint3 = & smartPoint2; Student * students2; students2 = smartPoi Nt3-> Release (); // Release smartpoint to save the original address students2-> ShowAge (); return 0;} combining the two smart point examples, the usage is "every time you use the new operator to create an object, immediately assign the result to a smart pointer ". The above two smart points have not actually solved a problem, that is, when our object is const, what should we do? To analyze this situation, the object passed into the smart point is a const, which means that some member variables here cannot be modified, so the following semi-Intelligent smart point is available: scpp_ptr.h: [cpp] # ifndef _ SCCP_PTR_H _ # define _ SCCP_PTR_H _ # include "scpp_assert.h" namespace scpp {template <typename T> class Ptr {public: explicit Ptr (T * p = NULL): ptr _ (p) {} T * Get () const {return ptr _;} ptr <T> & operator = (T * p) {ptr _ = p; return * this;} T & operator * () const {SCPP_TEST_ASSERT (ptr _! = NULL, "Attempt to use operator-> on NULL pointer."); return * ptr _;} T * operator-> () const {SCPP_TEST_ASSERT (ptr _! = NULL, "Attempt to use operator-> on NULL pointer."); return ptr _;}~ Ptr () {} private: T * ptr _;} // namespace scpp # endif // _ SCCP_REFCOUNTPTR_H _ test code (vs2012 + win7 environment ): [cpp] # include "stdafx. h "# include" scpp_assert.h "# include" iostream "# include" scpp_vector.h "# include" inline "# include" scpp_matrix.h "# include" algorithm "# include" scpp_types.h "# include" scpp_refcountptr.h "# include" scpp_scopedptr.h "# include" scpp_ptr.h "# define STUDENTAGE 10 class Student {public: student (int age): age _ (age) {} void ShowAge () {std: cout <"my age is:" <age _ <std :: endl;} int GetAge () const {return age _;} void SetAge (int age) {age _ = age;} private: int age _;}; int _ tmain (int argc, _ TCHAR * argv []) {Student * student1 = new STUDENTAGE); // @ test: operator-> (T * p) scpp:: Ptr <Student> smartPoint1; smartPoint1 = student1; smartPoint1-> ShowAge (); Student * student2 = new STUDENTAGE); // @ test: operator * (T * p) scpp: Ptr <Student> smartPoint2; smartPoint2 = student2; (* smartPoint2 ). showAge (); Student * student3 = new STUDENTAGE (STUDENTAGE); // @ test: operator * (T * p) scpp: Ptr <const Student> smartPoint3; smartPoint3 = student3; std: cout <"this student age is:" <smartPoint3-> GetAge () <std: endl; smartPoint3-> SetAge (STUDENTAGE ); // @ cannot be used because it is restricted, and return 0 is not required ;}

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.