C language and memory alignment structure

Structure

Structure is a new data type, data type C language was greatly expanded.

struct STU{
  int age;
  char name[15];
 };

 struct STU  a;   //结构体实例
 struct STU  *b;  //结构体指针

1, can be taken by a.age its operation members, b-> age can also be operated structure, b-> age there is a problem, there must be a structure of space has been let to point b, b values for this address of the structure of space.
2, a.name = "lilei"; false, because the name is the name of the array, pointer constants can not be assigned, Solution: strcpy () function
3, strings directly comparable, then, is to compare their addresses, it does not make sense, with strcmp () function for comparison, is a comparison of the ASCII code.

Memory Alignment

The operating system for allocating memory space, the following principles: allocating space always starts from byte 2 ^ n for the multiple addresses.
: Start if the spatial distribution pattern thereof according 4B, then each variable (structure member) from the first address number is always an integer multiple of 4 bytes of.
Byte alignment may be provided as a few

#pragma pack(push)    //保持原对齐格式
#pragma pack(1)      //设定为n字节格式

 .......
 
#pragma pack(pop)     //恢复为原对齐格式

1, the size of the structure
(1), the total size is an integer multiple of a type otherwise filled with a single byte (one byte up to a maximum of 4/8, the specific structure of the widest see several bytes)
// the total size is the total number of bytes in front of the current and the number of bytes, and
(2), the size of the structure is the widest integer multiple of bytes (typically 4 or 8)
(3), the above-described two conditions must At the same time set up. If the end result is not established, then filled to an integral multiple of
2, the following example demonstrates the size of the structure
(1),

struct TEST{
    int a;
    short b;
    char c;
    struct TEST *next;
};

As follows: 4 -> 2 -> 1 (1 byte complement) -> 4 bytes Total 12
(2), borrow the structure (1)

struct TEST1{
  short d;
  int e;
  char f;
  struct TEST g;
  struct TEST1 *next;
  struct TEST h;
  char i;
};

As follows: 2 (s 2 bytes) -> 4 -> 1 (S 3 bytes) -> 12-> 4 (not want to fill up, to fill up the widest single byte, in which up to 4 characters section, has 4 bytes, and it is not in the fill) -> 12-> 1.
A total of: 41 bytes, the size of the structure is the widest single integer number of bytes, this should be an integer multiple of 4, and finally, the structure of this size is 44 bytes.
(3), borrow the structure (1)

struct TEST2{
  short d;
  int e;
  double f;
  struct TEST1 *next;
  struct TEST g;
  char i;
};

As follows: 2 (s 2 bytes) -> 4-> 8-> 4 (S 4 bytes: A total of 16 bytes in front of the current four bytes, so a total of 20 bytes, not the next data type (12) an integer multiple of 8 bytes and is the widest, can make up 4 bytes, constitute an integral multiple) -> 12-> 1.
A total of 37 bytes, the size of the structure is the widest single integer number of bytes, this should be an integer multiple of 8, the final structure of this size is 40 bytes.
Type structure greatly expanded the C language, a data type is more colorful.

Guess you like

Origin www.cnblogs.com/Hijack-you/p/11569549.html