sizeof usage (rpm)

This article includes two parts, the first part focuses on the VC, how about using sizeof to find the size of the structure, as well as prone to problems, and gives the solution to the problem, the second part summarizes the main uses of the VC sizeof .

. 1 , in the case of application of the sizeof structure

Consider the following structure:

struct MyStruct

{

double dda1;

good car;

int type

};

MyStruct structure using sizeof What will happen then? sizeof (MyStruct) is how much? Maybe you will find this:

sizeof(MyStruct)=sizeof(double)+sizeof(char)+sizeof(int)=13

But when testing the size of the structure above the VC, you will find the sizeof (MyStruct) 16. You will come to know why such a result in the VC it?

In fact, this is a special treatment VC variable storage. In order to improve storage speed of the CPU, VC of the starting address of some variables to do the "alignment" process. By default, VC respective predetermined start address stored in the member variable with respect to the start address of the configuration of a multiple of the number of bytes offset must be occupied for the type of the variable. The following lists the common types of alignment (vc6.0,32 bit system ) .

Types of

Alignment (start of this variable with respect to the starting address offset structure)

Char

Offset must be sizeof (char) that is a multiple of 1

int

Offset must be sizeof (int) that is a multiple of four

float

Offset must be sizeof (float) that is a multiple of 4

double

Offset must be sizeof (double) that is a multiple of 8

Short

Offset must be sizeof (short) 2 i.e. multiple

Members variables when stored sequentially in the order space applications appear in the structure, according to the above while adjusting the position of alignment, gap bytes VC is automatically populated. VC while the size of the structure in order to ensure (the number of bytes occupied by the largest type, i.e., the space occupied by the structure) is a structure of number of multiple-byte boundary, so after the last application of a member variable space, will automatically as needed filling the vacant bytes.

The following examples will be described using the foregoing VC in the end how to hold the structure.

struct MyStruct

{

double dda1;

good car;

int type

};

Space is allocated when the above structure, VC according to the order and alignment member variables appearing first as a member of the first dda1 allocated space, the same start address of the start address with the structure (just be exactly offset 0 sizeof (double) multiples), the member variable occupies sizeof (double) = 8 bytes; dda member next to the second distribution space, then the next address can be allocated to the starting address offset structure is 8, is a multiple of sizeof (char), it is stored in the offset dda 8 meet local alignment, the member variable occupies sizeof (char) = 1 byte; next to the third distribution space type member , then the next address can be allocated to the starting address offset structure 9 is not sizeof (int) = a multiple of 4, in order to satisfy the constraint problem of alignment offset, VC bytes autofill3 (this does not put anything three bytes), then the next address can be allocated to the starting address offset structure 12, just sizeof (int) = a multiple of 4, so the type stored in the offset the amount of local 12 Member variable occupies sizeof (int) = 4 bytes; In this case the entire structure member variables have been allocated space, the total space occupied as follows: 8 + 1 + 3 + 4 = 16, exactly byte structure number boundary (i.e., structure type occupy the space occupied by the maximum number of bytes sizeof (double) = 8) multiple, so there is no need to fill the vacant bytes. Therefore, the size of the entire structure is: sizeof (MyStruct) = 8 + 1 + 3 + 4 = 16, which is 3 bytes VC populated automatically, not put anything meaningful.

Here another example, to exchange the position of member variables above MyStruct, and turned it into the following conditions:

struct MyStruct

{

 good car;

 double dda1; 

 int type

};

This structure occupies space be? In VC6.0 environment can be obtained sizeof (MyStruc) 24. Combined with a number of principles to allocate space mentioned above, the VC analyzes how to allocate space for the construction of the above. (Brief description)

struct MyStruct

{

  char dda; // offset of 0, satisfy alignment, DDA occupies 1 byte;

 double dda1; // the offset address of the next available one is not sizeof (double) = 8

             // multiple of 7 bytes to be filled to the offset amount becomes 8 (to meet the alignment

             // mode), thus automatically filled VC 7 bytes, DDA1 stored at offset 8

             // on the address, it takes up 8 bytes.

 int type; // the offset address of the next available as 16, is sizeof (int) = 4 times the

           // number satisfying int alignment, so no auto-fill VC, deposit type

           // offset in the address 16, which is 4 bytes.

}; // all member variables are allocated space, the total size of the space is 1 + 8 + 4 + 7 = 20, instead of the structure

   // number of sections of the boundary (i.e., number of bytes occupied by the structure type sizeof maximum space occupied by

   // (double) = 8) a multiple of 4 bytes of padding is required to meet the size of the structure is

   // sizeof (double) = a multiple of eight.

 

Therefore, the total size of the structure: sizeof (MyStruc) 1 + 7 + 8 + 4 + 4 = 24. Wherein the total of 7 + 4 = 11 bytes are populated automatically VC, did not put anything meaningful.

 

VC storage structure for special treatment does improve the speed of the CPU to store a variable, but sometimes also brought some trouble, we also masked variables default alignment, you can set the alignment of variables.

VC provides #pragma pack (n) is set to the variable n byte alignment. n byte alignment that is stored in the start address of the variable offset in two cases: first, if n is greater than or equal to the variable number of bytes occupied, then the offset must satisfy the default alignment, the second If n is less than the number of bytes occupied by the type of the variable, then the offset is a multiple of n, does not fulfill the default alignment. The total size of the structure also has a constraint, the following two sub-cases: If n is greater than the number of bytes occupied by all members of the variable type, the total size of the structure must be the maximum space occupied by a multiple number of the variable space;

Otherwise it must be a multiple of n. The following example of its usage.

#pragma pack (push) // save aligned state

#pragma pack (4) // set to 4-byte alignment

struct test

{

  char m1;

  double m4;

  int m3;

};

#pragma pack (pop) // restore alignment status

The size of the above structure is 16, which stores the following analysis, the first allocation space for the m1, an offset of 0, satisfying our own set of alignment (4-byte alignment), m1 occupies 1 byte. Then begins to allocate space m4, then an offset of 1, 3 bytes to be filled, so that the offset amount to satisfy n = a multiple of 4 (because sizeof (double) is greater than n-), m4 character occupies 8 section. Followed m3 allocate space, then an offset of 12, satisfying a multiple of 4, 4 bytes m3. In this case all member variables have been allocated space, allocated a total of 16 bytes is a multiple of n is satisfied. If the above #pragma pack (4) to #pragma pack (16), then we can get the size of the structure 24 . (Reader own analysis)

2 , sizeof usage summary

In the VC, sizeof has many uses, and can easily lead to mistakes. Here be a summary based on the usage of parameters behind the sizeof sizeof of.

A. Or a data type parameter is a general variable. For example, sizeof (int), sizeof (long) and the like. This situation should be noted that the results of different systems or systems obtained by different compilers may be different. E.g. int is 2 bytes in the 16-bit system, four bytes in the 32-bit system.

B. Parameter is an array or a pointer. Exemplified below.

int a [50]; // sizeof (a) = 4 * 50 = 200; space size occupied by the array seek

int * a = new int [50]; // sizeof (a) = 4; a is a pointer, sizeof (a) is a pointer to seek

                   // size of the 32-bit system, of course, is 4 bytes.

C. Or a class structure parameter. Sizeof Application and handling of the class structure is the same. But there are two points to be noted, first, a static structure or class structure or class member does not affect the size, because the storage location independent of the static variable or a class instance address structure.

Second, the size of the member variables for a class or structure, since it is necessary to ensure that each type of structure or

Instance has a unique address in memory.

Illustrated below,

Class Test{int a;static double c};//sizeof(Test)=4.

Test * s; // sizeof (s) = 4, s is a pointer.

Class test1{ };//sizeof(test1)=1;

D. For other parameters. Exemplified below.

   int func(char s[5]);

   {

     cout << sizeof (s); // output 4 where s is originally an array, but the function as

                     // when the number of parameters in the transmission processing system is a pointer, the

                     In // sizeof (s) actually required size of the pointer.

     return 1;

}

sizeof (func ( "1234")) = 4 // for func because the return type int, so the equivalent

                     //求sizeof(int).

 

The above is the basic usage of sizeof, in actual use should pay attention to the analysis of the distribution of variables VC allocation policy, so to avoid some mistakes

Reproduced in: https: //www.cnblogs.com/MichaelGuan/archive/2009/03/13/1410905.html

Guess you like

Origin blog.csdn.net/weixin_34179968/article/details/93292399