In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is C++ high-quality programming?", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is C++ high-quality programming?"
You need a function to assign an array to an isometric sequence and will use it outside the function.
Unreasonable:
Int * GetArray (int n) {int * p = new int [n]; for (int I = 0; I < n; iTunes +) {p [I] = I;} return p;}
Reasonable:
Void GetArray (int * p, int n) {for (int I = 0; I < n; iTunes +) {p [I] = I;}}
Parsing:
The * method to check memory leaks is to check the application and release of complete pairing, which is applied in the function and released externally, which will lead to poor code consistency and difficult to maintain. Moreover, the function you write is not necessarily for your own use, others will not know how to properly use such a function, if it is a DLL export function, and you use it on different platforms, it will cause the system to crash. * the solution is to apply for memory outside the function call, and the function only copies the data.
Second, you need to write a class to manage a pointer for you, this class will encapsulate the application memory, release and other basic operations to the pointer.
Unreasonable:
Class A {public: a (void) {} ~ A (void) {delete [] mroompPtra;} void Create (int n) {m_pPtr = new int [n];} private: int * mfolpPTrr;};
Reasonable:
Class A {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 [] MPT; m_pPtr = 0;} private: int * mPTrr;}
Parsing:
The unreasonable code is that when you call Create repeatedly, it will cause a memory leak. The solution is to determine whether the pointer is 0 before new. To perform this judgment effectively, you must initialize the pointer at construction time and add a Clear function to the class to free memory.
Third, the Create function of the previous question, you now need to do some complex algorithm operations according to the passed 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; iTunes +) {mpPtra [I] = 3 / a [I];} return true;}
Reasonable:
Template class auto_array {public: explicit auto_array (_ Ty* pPtr=0) throw (): m_Ptr (pPtr) {} ~ auto_array () {delete [] mPtrr;} void reset (_ Ty* pPtr=0) {if (pPtratermittr) {delete [] mPtritsPtrittPtre;}} _ 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 * mendPtra;}; bool A::Create (int * a, int n) {if (m_pPtr) return false; auto_array PtrGuard (new int [n]); for (int I = 0; I < n; iPrep +) {if (0 = = a [I]) {return false } PtrGuard .get () [I] = 3 / a [I];} m_pPtr = PtrGuard.release (); return true;}
Parsing:
In the loop, when one of the values in the parameter array an is 0, a division exception will be generated, which will result in an unreasonable release of the memory you requested for m_pPtr above. To solve this problem, we wrote an auto_array as a guard to guard the pointer in an attempt to escape. When the auto_array object PtrGuard is destructed, it also removes the memory pointers attached to it. We first use PtrGuard to do all the pointer operations, determine the complete end of the operation * *, assign the pointer to the real variable, and make PtrGuard abandon the attachment to the pointer, so we get the safest result. In addition, it should be noted that C++ 's STL library originally has a template class auto_ptr that is very similar to auto_array, but it only supports the memory of a single object, not an array, so writing such an auto_array is a last resort.
Fourth, you need to open up a section of memory to store and manage a 4 x 4 matrix and unify it.
Unreasonable:
Int aMatrix [4] [4]; for (int I = 0; I < 4; iTunes +) {for (int j = 0; j < 4; jaun +) {if (I = = j) {aMatrix [I] [j] = 1;} else {aMatrix [I] [j] = 0;}
Reasonable:
Int aMatrix [4 * 4]; for (int I = 0; I < 4; iTunes +) {for (int j = 0; j < 4; jaun +) {if (I = = j) {aMatrix [I * 4 + j] = 1;} else {aMatrix [I * 4 + j] = 0;}
Parsing:
At any time to avoid the use of multi-dimensional array, the increase of array dimension, the corresponding program complexity will increase in a geometric series, but also more difficult to understand.
You need to assign a value to the above matrix so that it is assigned from the upper left corner to the lower right corner in the order of vertical and then horizontal.
Unreasonable:
For (int I = 0; I < 4; iTunes +) {for (int j = 0; j < 4; jacks +) {aMatrix [j * 4 + I] = I * 4 + j;}}
Reasonable:
For (int I = 0; I < 4; iTunes +) {for (int j = 0; j < 4; jacks +) {aMatrix [I * 4 + j] = j * 4 + I;}}
Parsing:
Try to make sure that every element of the array is accessed sequentially. Because of the management mode of Windows memory, memory is managed by pages. Sequential access array can basically ensure that the page will not switch back and forth, thus reducing the number of page failures and improving the overall performance of the program. This performance improvement is particularly pronounced for large arrays.
6. You need to use three float values to represent a three-dimensional point, and write a function to calculate and assign an array of three-dimensional points.
Unreasonable:
Void foo (float * pPoints [3]) {float aPoint [3] = {1.0f, 2.0f, 3.0f}; int nCount = (int) _ msize (pPoints); for (int I = 0; I < nCount; I +) {pPoints [I] [0] = aPoint [0]; pPoints [I] [1] = aPoint [1]; pPoints [I] [2] = aPoint [2];}
Reasonable:
Struct POINT3 {float x, y, z;}; void foo (POINT3 * pPoints, int nCount) {POINT3 Pt = {1.0f, 2.0f, 3.0f}; for (int I = 0; I < nCount; ionization +) {pPoints [I] = Pt;}
Parsing:
First, do not use _ msize to measure the size of the array. _ msize can only measure the size of the memory applied for using malloc or calloc. For others, such as new or some API, it will cause the program to crash. When designing such functions that need to pass in an array, don't forget to pass in the number of elements of the array as a parameter, even if it is fixed, which will be a good habit.
Second, for the type of float [3], try to avoid using it directly. The way to * is to simply encapsulate it with struct. When copying, you can use "=" to assign accurate bitwise values.
7. You have a definition of a function in which you new a larger object, Data, and delete it after calculation. But this function will be called frequently.
Unreasonable:
Void foo (void) {Data * p = new Data; CalcData (p); delete p;}
Reasonable:
Char Buf [sizeof (DATA)]; void foo (void) {Data * p = new (Buf) Data; CalcData (p);}
Parsing:
New (buf) type; is a positioning new syntax, which does not really allocate memory, but simply divides a section of space that matches the type size on the specified starting point of the allocated memory, and directly constructs the object on this type of memory, and returns the pointer to the object. Because it does not really allocate memory space, so its efficiency is very high, similar to the above routines, frequently apply for and release a large object operation, positioning new can bring great efficiency improvement.
At this point, I believe that everyone on the "C++ high-quality programming what" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.