The length of the char type is defined as a 8-bit byte, which is simple.
the short type is at least two bytes long. on some computers, for some compilers, the short type may be 4 bytes long or longer.
The int type is the "natural" size of an integer, which is at least two bytes in length and is at least as long as the short type. on a 16-bit computer, the int type may be two bytes long, or 4 bytes on a 32-bit computer, and the length of the int may reach 64 bytes when the 8-bit computer is popular. This is all about "possible", for example, early Motorala 68000 is a 16/32-bit hybrid computer that relies on different command-line options, and a 68000 compiler can produce two-byte or 4-byte-long int types.
The long type is at least as long as the int type (so it is at least as long as the short type). a long type is at least 4 bytes long. The compiler on a 32-bit computer may make both the short,int and long types 4 bytes long-or not.
Members in a struct can be of different data types, and members are stored sequentially in contiguous memory space in the order in which they are defined. Unlike arrays, the size of a struct is not a simple addition to the size of all members, and it is necessary to take into account the problem of address alignment when the system is storing structural variables.
Understand and learn how to calculate the size of a struct by using the following example to calculate the size of the struct body.
struct STU
{
int i;
char c;
Int J;
};
First, we introduce a related concept-offset. Offset refers to the difference between the address of a member in a struct variable and the address of a struct variable. Then give the definition of the struct size: The structure size is equal to the last member's offset plus the size of the last member. Obviously, the address of the first member of a struct variable is the first address of a struct variable. Therefore, the first member I has an offset of 0. The offset of the second member, C, is the offset of the first member plus the size of the first member (0+4), its value is 4, the offset of the third member J is the offset of the second member plus the size of the second member (4+1), with a value of 5.
In fact, because of the requirement of address alignment when storing variables, the compiler follows three principles when compiling a program:
The offset of a member in a struct variable must be an integer multiple of the size of the member (0 is considered to be an integer multiple of any number).
The structure size must be an integer multiple of the size of all members.
Third, if the structure of the member is another structure, just expand it. One thing to note, however, is that the offset of the first member of the expanded struct should be an integer multiple of the largest member in the expanded struct body. Note: The above three principles apply only to VC,GCC there are different mechanisms for memory alignment.
The first two members of the above example have an offset of 5, but the third member is not an integer multiple of its own (int) size, compared with the previous one. When processing, the compiler will fill 3 empty bytes after the second member, making the offset of the third member 8.
In contrast to the second, the struct size equals the last member's offset plus its size, the above example calculates a size of 12, satisfies the second principle, so the size of the struct is 12.
Look at a situation that satisfies the first rule and does not satisfy the second.
struct stu_2
{
int k;
Short T;
};
The offset of member K is 0, and the member T has an offset of 4, which does not need to be adjusted. But the calculated size is 6, which is obviously not an integer multiple of the member K size. Therefore, the compiler will make up 2 bytes after the member T, making the size of the struct 8 to meet the second requirement. So the size of the structure is 8.
This shows that when you define struct types, you need to take into account the case of byte alignment, which affects the size of the structure in different order. Compare the following two kinds of definition order.
struct STU_3
{
Char C1;
int i;
Char C2;
};
struct STU_4
{
Char C1;
Char C2;
int i;
};
Although the members of the struct STU3 and Stu4 are the same, the value of sizeof (struct STU3) is 12 and sizeof (struct STU4) has a value of 8.
How should a member of a struct be evaluated when it is another struct type? Just expand it. One thing to note, however, is that the offset of the first member of the expanded struct should be an integer multiple of the largest member in the expanded struct body. Look at the following example:
struct STU_5
{
Short I;
struct
{
char c;
Int J;
}SS;
int k;
};
The offset of the member ss.c of the struct STU5 should be 4, not 2. The entire structure size should be 16.
How to allocate space for a struct variable is determined by the compiler, which is for VC 6.0 under Windows XP. The C compiler for other platforms may have different processing.
Example code:
#
include<Stdio.h>
structStu
{
intI
CharC
intK
};//size is
structStu_2
{
intK
ShortT
};//size is 8
structStu_3
{
CharC1;
intI
CharC2;
};//size is
structStu_4
{
CharC1;
CharC2;
intI
};//size is 8
structStu_5
{
ShortI
struct
{
CharC
intJ
} SS;
intK
};//size is
structStu_6
{
intI
CharJ
CharK
};//size is 8Transferred from: http://www.cnblogs.com/ppboy_dxh/archive/2013/08/21/3273376.html
To calculate the structure body size