High-quality C ++ programming (2)

Source: Internet
Author: User

Use reasonable memory access methods

1. You need to open up a piece of memory to store and manage a 4x4 matrix, which is then unitized.

Unreasonable:

Int aMatrix [4] [4];

For (int I = 0; I <4; I ++)

{

For (int j = 0; j <4; j ++)

{

If (I = j)

{

AMatrix [I] [j] = 1;

}

Else

{

AMatrix [I] [j] = 0;

}

}

}
 
Reasonable:


Int aMatrix [4*4];

For (int I = 0; I <4; I ++)

{

For (int j = 0; j <4; j ++)

{

If (I = j)

{

AMatrix [I * 4 + j] = 1;

}

Else

{

AMatrix [I * 4 + j] = 0;

}

}

}
 
Resolution:

Avoid using multi-dimensional arrays at any time. As the array dimension increases, the complexity of the corresponding program will increase in the form of geometric series, which is more difficult to understand.

2. You need to assign values to the matrix above so that it can be assigned in the vertical and horizontal order from the upper left corner to the lower right corner.

Unreasonable:


For (int I = 0; I <4; I ++)

{

For (int j = 0; j <4; j ++)

{

AMatrix [j * 4 + I] = I * 4 + j;

}

}
Reasonable:

For (int I = 0; I <4; I ++)

{

For (int j = 0; j <4; j ++)

{

AMatrix [I * 4 + j] = j * 4 + I;

}

}
 
Resolution:

Try to ensure that each element of the array is accessed in sequence. Because of the Windows Memory Management Mode, the memory is managed by page. Sequential access to the array can basically ensure that the page does not switch back and forth, thus reducing the number of page failures and improving the overall performance of the program. This performance improvement is especially evident for large arrays.

3. You need to use three float values to represent a 3D vertex, and write a function to calculate and assign values to an array of 3D vertices.

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; I ++)

{

PPoints [I] = Pt;

}

}
 
Resolution:

There are two points, one, do not use _ msize to determine the size of the array, _ msize can only be used to determine the size of the memory applied for using malloc or calloc, for other such as new or some APIs, the program will crash. When designing a function that needs to pass in an array, do not forget to pass in the number of elements of the array as a parameter, even if it is fixed, this will be a good habit. 2. For float [3], try to avoid using it directly. The best way is to use struct for simple encapsulation, when copying, you can directly use "=" to accurately assign values by bit.

4. You have a function definition. In this function, a large object Data is added and deleted after computation. However, 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 );

}
 
Resolution:

New (buf) type; is the new syntax for positioning. It does not actually allocate memory, but simply Splits a space that matches the type size on the specified allocated memory start point, and construct an object of this type directly in the memory, and return the object pointer. Because it does not actually allocate memory space, its efficiency is very high. Similar to the preceding routine, it frequently applies for and releases operations on a large object, new positioning can greatly improve efficiency.

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.