Analysis of sizeof in C language

  • 2020-04-02 01:07:55
  • OfStack

This is a value that depends on the compilation system, one
General definition is typedef unsigned int size_t; The compiler is always there, but as a specification, it guarantees char, signed
Char and unsigned char have a sizeof value of 1. After all, char is the smallest data type that can be programmed.
The interpretation on MSDN is as follows:
Sizeof keyword gives The amount of storage, in bytes, associated with avariable ora
Type (including aggregate types). This keyword returns a value of type
Size_t.

2. Grammar:
Sizeof has three grammatical forms, as follows:
1) the sizeof (object); // sizeof;
2) the sizeof (the type_name); // sizeof;
3) the sizeof the object; // sizeof object;
So all three sizeof USES are correct


#include <stdio.h>
main()
{
int b;
printf("%dn",sizeof b);
printf("%dn",sizeof(b));
printf("%dn",sizeof(int));
}

4. Sizeof of basic data type
The basic data types here refer to simple built-in data types such as short, int, long, float, and double, since they are sum strings
It's all correlated, so it might be different in different systems, so it's important that we pay attention to that, and try not to
This aspect causes problems for the migration of their own programs. Typically, sizeof(int) is 4 in a 32-bit compilation environment.

Sizeof of pointer variables
Is equal to the width of the internal address bus of the computer. So in a 32-bit computer, the return value of a pointer variable must be 4
Bytes in units), you can expect sizeof to result in a pointer variable of 8 in a future 64-bit system.
The sizeof value of the pointer variable has nothing to do with the object the pointer refers to, because all pointer variables take up the same amount of memory
The MFC message handler USES two parameters, WPARAM and LPARAM, to pass a variety of complex message structures
Pointer to a structure.

6. Sizeof of array
The sizeof value of the array is equal to the number of bytes occupied by the array, such as:
Char a1 [] = "ABC";
Int a2 [3].
Sizeof (a1); The result is 4, and there is a NULL terminator at the end of the character
Sizeof (a2); // 3*4=12 (dependent on int)
Int c1 = sizeof(a1) int c1 = sizeof(a1) int c1 = sizeof(a1)
/ sizeof (char); // total length/length of a single element
Int c2 = sizeof(a1)/sizeof(a1[0]); // total length/length of the first element
Is reduced to pointer.

Sizeof the structure
Struct S1
{
Char c;
Int I;
};
The result of sizeof is equal to the number of bytes in memory taken up by the object or type. Well, let's look at the memory allocation for S1: S1, S1
= {'a', 0xFFFFFFFF}; The address of s1 is 0x0012FF78, and its data contents are as follows:

0012ff78:61 CC CC CC FF FF FF FF with 3 bytes of CC in the middle look at the description on MSDN: When applied to a
Structure type or variable, sizeof returns the actual size, which may
Include padding bytes inserted for alignment.

This is byte alignment! Why do you need byte alignment the principles of computer composition teach us that this helps speed up the retrieval of Numbers, otherwise
It's going to take more instruction cycles. To do this, the compiler, by default, processes the structure (actually the number elsewhere)
The same is true for data variables), so that basic data types (short, etc.) of width 2 are located at addresses divisible by 2, and base data types of width 4 are located at addresses divisible by 2
The data type (int, etc.) is at an address divisible by 4, and so on. So it's possible to go between the two Numbers
The fill bytes need to be added, so the sizeof value for the entire structure grows.

1. Sizeof is the operator Is the same as addition, subtraction, multiplication and division, and is performed at compile time, not at run time.
So what if you verify that in your programming?


<SPAN style="FONT-SIZE: 18px">#include<iostream></SPAN><SPAN style="FONT-SIZE: 14px">

using namespace std;

int main()
{
    int i=1;
    cout<<i<<endl;
    sizeof(++i);
    cout<<i<<endl;
    return 1;
}</SPAN>

The input result is 1    
                            1
The side effect of ++ I in sizeof is not shown, there is only one possible reason, the side effect of ++ I was processed after sizeof was executed at compile time, so the side effect of ++ I was eliminated. If sizeof is done at run time, you should definitely pay attention to ++ I. Actually the implementation of sizeof should be done with macros, which are executed at compile time. Specific implementation can refer to the following.
2. Sizeof ('a') results in 4 in C and 1 in C++. I read an article that said sizeof in C focuses on "Numbers", while sizeof in C++ focuses on "characters".

3. Two classic applications of sizeof with macros are described in the article


//Applies to non-arrays
#define _sizeof(T) ((size_t)((T*)0 + 1))
//For arrays
#define array_sizeof(T) ((size_t)(&T+1)-(size_t)(&T))

Two small examples of two macros are given, for the first example, _sizeof(int); The result is 4; For the second, first declare an array int a[4] of size 4. So array_sizeof(a) is equal to 16.

For a non-array macro definition, you first convert 0 to the address to which a pointer of type T* points (at this point, the address is 0). Then add 1 to the address of type T, which is equivalent to adding the size of type T. The previous size_t just returns the address as an int.
A simple example: int* p; P = p + 1; --p is an int* pointer, and p+1 adds four bytes to the address space.

For the macro definition of the array, similar to the macro definition of the non-array, for the sake of understanding, you can think of the array T as a user-defined type, &T represents the pointer to the array type, for the array type pointer plus 1 is equivalent to adding the array size to the address. Since it is a user-defined type, you cannot force 0 to be converted to an address of array type. You can only subtract the previous address from the address after adding 1, and the difference is the size of the byte occupied by the array itself.


Related articles: