ALIGN problem in CC ++

xiaoxiao2021-03-06  57

In C / C programming, there is an align problem that is often ignored. The so-called Align problem means that the CPU can achieve better access efficiency by alignment of the original data type by boundary. The basic principle of Align is that the data type is aligned as a border in its own. For example, Char is aligned with 1byte, and Short Press 2Byte alignment, and int is aligned with 4Byte. When defining structure, union, class, you need to take into account this align problem. If you have access to UNALIGNED data, different CPUs and different OS have different processing, especially paying attention to this problem in multi-platform development. X86 has handled access to UNALIGNED data, but it has an impact on performance. Some RISC type CPUs are accessible when accessing the data of Unaligned, and some of the CPUs of the RISC type will issue an exception to the OS, which is determined by the OS. In summary, access to UNALIGNED data has the following four situations: 1, CPUs are handled in performance, and access to UNALIGNED data. 2, the CPU throws an exception to the OS, OS access to UNALIGNED data. 3. The CPU throws an exception is not processed by the OS, which will generate a serious problem such as abnormal interruption. 4, the compiler processes the access of UNALIGNED data, which must also be at the expense of performance. For the compiler to process the data of UNALIGNED, in order to achieve the purpose of Align, the Pading data is automatically added. At this time, it is necessary to pay attention to these Pading data, especially when using a pointer, it usually obtains some expected results. Here is an example under VC : ========================================= ==================== # include "stdafx.h" #include

#pragma pack (1)

Struct s1 {char I; short j; int K;};

Struct S2 {char I; Struct S1 J;};

#pragma pack ()

/ ********************************** STRUCT S3 {char i; char _pad [1]; short j; int K;}; *** *********************** / STRUCT S3 {char i; short j; int K;};

/ ********************************** STRUCT S4 {char i; char _pad [3]; struct s3 j;}; ***** ********************* / STRUCT S4 {Char I; Struct S3 J;};

/ *********************************** STRUCT S5 {char i; char _Pad1 [3]; int K; short j; char _pad2 [2] ;}; *********************************** / STRUCT S5 {char I; INT K; short j;}; struct S1 T1; Struct S2 T2; STRUCT S3 T3; Struct S4 T4; Struct S5 T5;

INT Main (int Argc, char * argv []) {INT i; char * pi; short * pj; int * pk;

// 7 / ************************************** STRUCT S1 {char i; short j; int K;}; ***** ********************** / I = SizeOf (T1); Printf ("% d / n", i);

//8 / *********************************** Struct S2 {char i; struct s1 j;}; ******* ****************** / I = SizeOf (T2); Printf ("% d / n", i);

// 8 / *********************************** STRUCT S3 {char i; char _pad [1]; short j; int K;}; ************************** / I = SizeOf (T3); Printf ("% D / N", i);

// 12 / ************************************ Struct S4 {char i; char _pad [3]; Struct S3 J;}; ** ************************ / I = SizeOf (T4); Printf ("% D / N", I);

// 12 / ************************************ Struct S5 {char i; char _pad1 [3]; int K; short j; char _PAD2 [2];}; ************************* / i = SizeOf (T5); Printf ("% d / n", i );

T1.i = 1; t1.j = 2; t1.k = 3;

// 1, 2, 3 pi = & t1.i; pj = (short *) (pi 1); pk = (int *); PRINTF ("% D,% D,% D / N ", * pi, * pj, * pk);

T3.i = 1; t3.j = 2; t3.k = 3;

// 1, 512, 768 pi = & t3.i; pj = (short *); pk = (int *) (pi 3); Printf ("% D,% D,% D / N ", * pi, * pj, * pk);

Return 0;}

/ ************ OUTPUT ************ 78812121, 2, 31, 512, 768 *************** *************** / ============================================================================================================================================ =========================== 参考: http://msdn.microsoft.com/library/default.asp? URL = / library / en-us/vclang/html/_predir_pack.aspbret S. Pehrson: UNALIGNED DATA Access

Jady Leung September 25, 2004

转载请注明原文地址:https://www.9cbs.com/read-113175.html

New Post(0)