High quality c ++ programming (1) [Z]

Source: Internet
Author: User
Effectively solves Memory leakage
I,
You need a function to assign an array to an equal-difference sequence and use it outside the function.

Unreasonable:


Int * getarray (int n)

{

Int * P = new int [N];

For (INT I = 0; I <n; I ++)

{

P [I] = I;

}

Return P;

}

 

Reasonable:

Void getarray (int * P, int N)

{

For (INT I = 0; I <n; I ++)

{

P [I] = I;

}

}

 

Resolution:

The best way to check for Memory leakage is to check the application and release of the perfect pairing. Releasing the application in the function outside of the system will lead to poor code consistency and difficulty in maintenance.

In addition, the function you write is not necessarily used by yourself. Others may not know how to use this function properly. If it is a DLL export function, if you use it on different platforms, the system will crash. The best solution is to apply for memory outside the function call, and the function only replicates data.
II,You need to write a class to manage a pointer for you. This class will encapsulate the application memory for the pointer, release and other basic operations.

 Unreasonable:

Class

{

Public:

A (void ){}

~ A (void) {Delete [] m_pptr ;}

Void create (int n) {m_pptr = new int [N];}

PRIVATE:

Int * m_pptr;

};

 

Reasonable:

Class

{

Public:

A (void): m_pptr (0 ){}

~ A (void) {clear ();}

Bool create (int n) {If (m_pptr) return false; m_pptr = new int [N]; return ture ;}

Void clear (void) {Delete [] m_pptr; m_pptr = 0 ;}

PRIVATE:

Int * m_pptr;

};

 

Resolution:

The unreasonable code is that when you repeatedly call create, it will cause memory leakage. The solution is to determine whether the pointer is 0 before new. To effectively execute this judgment, you must initialize the pointer during the construction and add a clear function for this class to release the memory.
III,Now you need to perform complex algorithm operations based on the input parameters and assign values to the applied array.

Unreasonable:

Bool create (int * a, int N)

{

If (m_pptr)

Return false;

M_pptr = new int [N];

For (INT I = 0; I <n; I ++)

{

M_pptr [I] = 3/A [I];

}

Return true;

}

 

 Reasonable:

Template <class _ ty>

Class auto_array

{

Public:

Explicit auto_array (_ ty * pptr = 0) Throw (): m_ptr (pptr ){}

~ Auto_array () {Delete [] m_ptr ;}

Void reset (_ ty * pptr = 0) {If (pptr! = M_ptr) {Delete [] m_ptr; m_ptr = pptr ;}}

_ Ty * release (void) {_ ty * ptemp = m_ptr; m_ptr = 0; return ptemp ;}

PRIVATE:

Auto_array (const auto_array & Other ){}

Auto_array & operator = (const auto_array & Other ){}

_ Ty * m_ptr;

};

Bool a: Create (int * a, int N)

{

If (m_pptr)

Return false;

Auto_array <int> ptrguard (New int [N]);

For (INT I = 0; I <n; I ++)

{

If (0 = A [I])

{

Return false;

}

Ptrguard. Get () [I] = 3/A [I];

}

M_pptr = ptrguard. Release ();

Return true;

}

 

 Resolution:

In a loop, when a value in parameter array a is 0, an exception will occur except 0, this will cause the memory you applied for m_pptr to be released improperly. To solve this problem, we wrote an auto_array as a guard to guard the pointer attempting to escape.

When the auto_array object ptrguard is parsed, it also deletes the memory pointer attached to it. We first use ptrguard to perform all pointer operations. At the end of the operation, we assign the pointer to the real variable and let ptrguard discard the appending of the pointer, in this way, we get the safest result.

Note that the STL library in C ++ originally has a template class auto_ptr that is very similar to auto_array, but it only supports the memory of a single object and does not support arrays, writing such an auto_array is a last resort.
 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 222995

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.