sizeof receives pass participation usage

1. The definition of sizeof - itself is an operator not a function

        1.1. You can think of operators as built-in, most basic functions, which cannot be completely replaced by several functions that do not use the same type of operator. Like the addition operator, you can't write a function without + or - that does addition in any case.

        1.2. There is another important difference between an operator (a type of operator) and a function. The function itself has a piece of code. When the program executes, when it encounters a function, it will first push the function's parameters onto the stack, and then jump to the function's code to run. Operators operate directly locally.

2. Syntax of sizeof

        2.1 . Syntax: sizeof has three syntax forms, as follows:

            1) sizeof( object ); // sizeof( 对象 );
            2) sizeof( type_name ); // sizeof( 类型 );

            3) sizeof object; // sizeof object;

        2.2 Incoming parameters (only important arrays and structures)

            2.2.1  The sizeof value of an array is equal to the number of bytes of memory occupied by the array

         char a1[] = "abc";

         int a2[3];

         sizeof( a1 ); // the result is 4, there is a NULL terminator at the end of the character

         sizeof( a2 ); // result is 3*4=12 (depending on int)

         Some friends used sizeof as the number of array elements at the beginning. Now, you should know that this is wrong, so how to find the number of array elements? Easy, usually there are the following two ways of writing:

        int c1 = sizeof( a1 ) / sizeof( char ); // total length / length of a single element

        int c2 = sizeof( a1 ) / sizeof( a1[0] ); // total length / length of first element

        Write here, ask a question, what should the following c3 and c4 values ​​be?

        void foo3(char a3[3])

        {

                int c3 = sizeof( a3 ); // c3 == 4

        }

        void foo4(char a4[]) //When passing parameters, the formal parameter does not need to specify the size of the array, because the array needs to be specified when it is defined by "passing address"

        {

                int c4 = sizeof( a4 ); // c4 == 4

        }

        c3!=3. Here, the function parameter a3 is no longer an array type, but has transformed into a pointer, which is equivalent to char* a3. It is not difficult to understand why if you think about it carefully, when we call the function foo1, the program will allocate a size 3 on the stack. Arrays? No! The array is "by address", and the caller only needs to pass the address of the actual parameter, so a3 is naturally a pointer type (char*), and the value of c3 is 4.

        2.2.2  The sizeof of the structure, which is why I wrote this blog

                (1) This involves an important knowledge point - byte alignment

                   Explanation: Why do you need byte alignment? The principle of computer composition, because of the cooperation between the memory hardware composition and computer instructions, in order to facilitate the manufacture of memory hardware and simplify the instructions, byte-aligned access helps to speed up the computer's fetching speed, otherwise it has to be More instruction cycles are spent.

                             For this reason, the compiler handles the structure by default (in fact, the same is true for data ), so that the basic data types (short, etc.) with a width of 2 are located at addresses that are divisible by 2, and the width is The basic data types of 4 (int, etc.) are all located at addresses that are divisible by 4, and so on. In this way, padding bytes may need to be added between the two numbers, so the sizeof value of the entire structure increases.
        (2) If you need to know more about the structure, please refer to the blogger as follows, which is just written for my own needs



Reference blogger: https://blog.csdn.net/wzy198852/article/details/7246836



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856206&siteId=291194637