1

Summary

1) The size of the empty structure behaves differently under different compilers: the size of the empty structure under the gcc compiler is 0; the bcc and vc compilers think that the empty structure has a syntax error and a compilation error

2) The main points of flexible arrays:

struct SoftArray
{
    int len;
    int array[];
};

sizeof(struct SoftArray) = 4

    struct SoftArray* sa = NULL;
    
    sa = (struct SoftArray*)malloc(sizeof(struct SoftArray) + 5*sizeof(int));

    sa->len = 5;
  • The last array of flexible array is an identifier, does not occupy space, and sizeof(struct SoftArray) gets the space occupied by the length of the array.
  • The flexible array needs to be applied for from the heap space, and the array of the desired size can be allocated freely, and the size of the array can be specified after malloc
  • The advantage is that there is no need to specify the size of the array when defining it. can be used to specify the size at runtime; has length information, so there is no need to specify the size of the array as a function parameter.

3) All members of the union share a section of memory, the size of which is the memory occupied by the largest member.

4) little-endian system: low address stores low-order data; big-endian system: low address stores high-order data.
Use union to determine the size of the system. Regardless of the large and small end systems, the value of uu.c is taken from the lower address in bytes.
image.png

struct and union analysis

1、struct

1.1 How much memory does an empty structure occupy?

struct TS
{
};

sizeof(struct TS) = ?

The above code has different results under different compilers:

  • gcc compiler, the size of the empty structure is 0
  • bcc compiler, compilation error
  • vc compiler, compilation error

This corresponds to two interpretations:

  • Bcc and vc believe that since the structure is a collection of storage variables, if nothing is placed in a structure, it is naturally wrong and the compilation error
  • gcc believes that there are no variables in an empty structure, and the natural size is 0; at the same time, the empty structure type can also be used to define variables, and the size is also 0.

1.2 Flexible array

  • flexible array is an array whose size is to be determined
  • In C language, flexible arrays can be generated from structures
  • The last element of the structure in C language can be an array of unknown size

    struct SoftArray
    {
      int len;
      int array[];
    };
    
    sizeof(struct SoftArray) = 4

Usage of flexible array:

struct SoftArray
{
    int len;
    int array[];
};
int main()
{
    printf("sizeof(struct SoftArr) = %d\n", sizeof(struct SoftArray));

    struct SoftArray* sa = NULL;
    
    sa = (struct SoftArray*)malloc(sizeof(struct SoftArray) + 5*sizeof(int));

    sa->len = 5;

    for(int i=0; i<sa->len; ++i)
    {
        sa->array[i] = i+1;
        printf("sa->array[%d] = %d\n", i, sa->array[i]);
    }
        
    return 0;
}
  • The last array of flexible array is an identifier, does not occupy space, and sizeof(struct SoftArray) gets the space occupied by the length of the array.
  • The flexible array needs to be applied from the heap space of , and the array of the desired size can be allocated freely, and the size of the array can be specified after malloc
  • The advantage is that there is no need to specify the size of the array at the time of definition, but can be used to specify the size at runtime; with length information, so there is no need to specify the size of the array as a function parameter.

2、union

2.1 Memory occupied by union

  • Union in C language is syntactically similar to struc
  • Union only allocates space for the largest member, and all members share this space.

    #pragma pack(4)
    struct Struct
    {
      int a;
      double b;
      char c;
    };
    
    union Union
    {
      int a;
      double b;
      char c;
    };
    
    int main()
    {
      printf("sizeof(struct) = %d\n", sizeof(struct Struct));   // 16
      printf("sizeof(union) = %d\n", sizeof(union Union));  // 8

2.2 System big and small

union Union
{
    int i;
    char c;
};

int main()
{
    union Union uu;
    uu.i = 1;
    
    if(uu.c == 1)
        printf("Little ednian.");
    else
        printf("Big ednian.");

    return 0;
}

If it is a little-endian system, the low address stores the low-bit data, the "1" in this memory will be placed at the low address, so the value of c (1 byte) is 1;
If it is a big-endian system, the low address stores the high-bit data, the "1" in this memory will be placed at the high address, so that the value of c (1 byte) is 0.

This article is summarized from "C Language Advanced Course" by Tang Zuolin from "Ditai Software Academy".
If there are any errors or omissions, please correct me.


bryson
169 声望12 粉丝