C interview structure data size

        In C language-related interviews, the investigation of the size of the structure can reflect a programmer's understanding of memory allocation. It will involve the growth direction of the stack (that is, the high-order address is on the top, and the low-order address is on the bottom) and the direction of memory growth (big and small ends), as well as the memory ratio of various data types.

        A structure is a combination of various members, and its size can be generally understood as the sum of the sizes of each member .

        This article takes a 32-bit system as an example. In the VS environment, you can test and verify it yourself in the Linux system.

Table of contents

1. The size of the basic data structure

2. Structure memory allocation principle

3. Example demo

4. Summary


1. The size of the basic data structure

        To understand the memory allocation of a structure, you must first understand the size of the commonly used data types. The size of each type of data together constitutes the size of the structure.

basic data type Memory size in VS
short int 2 bytes
int 4 bytes
float 4 bytes
double 8 bytes
char 1 byte
int*/char*/pointer 4 bytes

        For example:

#include<stdio.h>

struct stu {
    int a;
    int b;
};

int main
{
    struct stu a;
    printf("%d\n",sizeof(a)); //4+4=8
    
    return 0;
}

2. Structure memory allocation principle

        After knowing the size of the basic data type, the size of the structure can be judged according to the principle of structure memory allocation.

        (1) If the number of bytes in the member is large, open up memory in units of its size

For example, there is only char data         in the structure members , and the memory is allocated in units of 1 byte. The size of this structure is a multiple of 1.

        There are char and int types in the members, and the int type data occupies the largest byte, and the memory is opened up in units of 4 bytes. The size of this structure is a multiple of 4.

        (2) Byte alignment

        The char-type data in the structure is 1- byte aligned, that is, to store char -type variables, and the number of the memory unit is a multiple of 1 .

        The int type data in the structure is 4-byte aligned, that is, the int type variable is stored, and the number of the starting memory unit is a multiple of 4 .

3. Example demo

        When the structure is allocated memory, it is allocated from the beginning according to the data sequence of the structure.

#include<stdio.h>

struct stu{
    char a;
    short int b;
    int c;
}a;

int main()
{
    printf("%d\n",sizeof(a));

    printf("%p\n",&(a.a));
    printf("%p\n",&(a.b));
    printf("%p\n",&(a.c));

    return 0;
}

Print result:

sizeof(a) = 8

The address of aa is different from the address of ab by 2; the address of ab is different from the address of ac by 2.

The memory allocation is shown in the figure:

         The structure first opens up space according to the largest data, the largest is int type, and opens up 4 bytes of space.

        Starting from the first member a, the char type data a only needs 1 byte, and then the second data short int type needs 2 bytes of space and must follow the byte alignment rules, so 0x0000 0001 cannot be stored, and the short int type element b is stored at 0x0000 0002. At this time, the first 4 spaces opened up are used up .

        Then open up 4 bytes of space to continue storing data, and store the third data int type, which occupies exactly 4 bytes.

        Therefore, the entire structure needs 4+4=8 bytes of space to store three members.

        The memory space of the structure is opened up sequentially from the beginning. If the order of the elements in the above structure is modified, the size of the structure will be completely different.

#include<stdio.h>

struct stu{
    char a;
    int c;
    short int b;
}a;

int main()
{
    printf("%d\n",sizeof(a));

    printf("%p\n",&(a.a));
    printf("%p\n",&(a.c));
    printf("%p\n",&(a.b));

    return 0;
}

Print result:

sizeof(a) = 12

The address of aa is different from the address of ac by 4; the address of ac is different from the address of ab by 4.

The memory allocation is shown in the figure:

        This structure is also based on the int type to open up space.

        First open up 4 spaces to store the char type a member, follow the byte alignment rules, then open up 4 bytes to store the second member int type, and then open up 4 byte spaces to store the third member short int type. Therefore, the size of the structure is 4+4+4=12 bytes.

4. Summary

        The structure is a collection of basic data types. The size of the structure is stored in order to open up space. It follows the principle of byte alignment. It can be seen from byte alignment that in C language, space is exchanged for time to improve the reading efficiency of the CPU.

        If it is not byte-aligned, some data needs to be read multiple times, which reduces the reading efficiency. For example: if there is no byte alignment in the structure, char type and int type data, char stores 1 byte, int type is stored in the next byte, cpu can only read 3 bytes, and then reads the last byte in int next time, resulting in int type data that needs to be read twice to complete.

Guess you like

Origin blog.csdn.net/qrx941017/article/details/130948259