As long as I participate in the software development test C/C ++, almost all of them will involve the usage of sizeof (). I met it yesterday, and some will, but the real sizeof () I still don't understand the core. I went online today and saw the detailed description of sizeof () When I was bored. Now I will share it with you. ------------Sizeof---------------- Sizeof is generally in the form of sizeofobject), or sizeof var_char, but most programer is used to sizeof (). The object can be an expression or data type name. When the object is an expression, brackets can be omitted. Sizeof is a single-object operator. The operator indicates the number of bytes occupied by an object in the computer memory. Generally, different machines run different objects. Currently, almost all machines are 32-bit and rarely 16-bit, therefore, the exams are generally based on 32-bit Windows and linux. There are not many data types in C language. 1. integer: short, int, long (I have not considered the symbol problem). Generally, in C language, int Is two bytes, that is, 16 bits, range:-32768-32767, long is 4 bytes in the range of-2 ^ 32---2 ^ 32-1. When sizeof (int) is run on xp, output 4 will be generated. This is the reason for 32 bits. Sizeoflong) is also 4. as follows: # include "stdio. h"
# Include "string. h"
# Include "stdlib. h"
Int main ()
{
Short int sa = 10;
Int a = 10;
Long la = 10;
Float f = 20;
Double d = 20;
Char ch = 'C ';
Char str [] = "ABC ";
Char * p = str;
Struct str {
Double d;
Char ch;
Int data;
} Str_wu;
Struct str1 {
Char ch;
Double d;
Int data;
} Str_wu1;
Printf ("sizeof (short): % d \ n", sizeof (sa ));
Printf ("sizeof (int): % d \ n", sizeof ());
Printf ("sizeof (long): % d \ n", sizeof (la ));
Printf ("sizeof (float): % d \ n", sizeof (f ));
Printf ("sizeof (double): % d \ n", sizeof (d ));
Printf ("sizeof (char): % d \ n", sizeof (ch ));
Printf ("sizeof (string): % d \ n", sizeof (str ));
Printf ("sizeof (point address): % d \ n", sizeof (p ));
Printf ("sizeof (Point): % d \ n", sizeof (* p ));
Printf ("sizeof (Struct): % d \ n", sizeof (str_wu ));
Printf ("sizeof (Struct): % d \ n", sizeof (str_wu1 ));
System ("pause ");
} 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1Z9144492-0.jpg "border =" 0 "/> SO int, short sizeof results are the same amount. 2. float data float, double, long double figure above, long double no test forget .....) Haha! But it should be 16. 3. For pointers, it is important to differentiate what data the Pointer Points to. The number of bytes it occupies in the memory is its result. For example, the pointer points to a string, which is the length of the string, because a character occupies one byte in memory. If the pointer points to a data structure, the result should be the memory bytes of the structured data. 4. In the above program,Struct str {
Double d;
Char ch;
Int data;
} Str_wu;
Struct str1 {
Char ch;
Double d;
Int data;
} Str_wu1;
There are two different structures, but the internal elements are the same. They are both double, int, and char. The results are different if the order is different. Why? At this time, because VC needs to store data, the specific situation is as follows: Type
Offset of the starting address of the alignment variable to the starting address of the structure) Char
The offset must be sizeof (char), that is, a multiple of int 1.
The offset must be sizeof (int), that is, a multiple of 4 float.
The offset must be sizeof (float), that is, a multiple of 4, double.
The offset must be sizeof (double), that is, a multiple of 8.
The offset must be sizeof (short), that is, a multiple of 2, for example, str_wu. When allocating space for the above structure, VC determines the order and alignment of the member variables, allocate space for the first member dda1. The starting address is the same as the starting address of the structure. The offset 0 is just a multiple of sizeof (double). This member variable occupies sizeof (double) = 8 bytes; next, allocate space for the second member dda. Then, the offset of the next allocable address to the starting address of the structure is 8, which is a multiple of sizeof (char, therefore, the dda is stored in an alignment where the offset is 8. This member variable occupies sizeof (char) = 1 byte, and then allocates space for the third member type, in this case, the offset of the next allocable address to the starting address of the structure is 9, not a multiple of sizeof (int) = 4. To meet the offset constraints of alignment, VC automatically fills in three bytes. The three bytes are not included.) At this time, the offset of the next allocable address to the starting address of the structure is 12, It is just a multiple of sizeof (int) = 4. Therefore, the type is stored in the place with the offset of 12. This member variable occupies sizeof (int) = 4 bytes; at this time, the member variables of the entire structure have been allocated space. The total occupied space is 8 + 1 + 3 + 4 = 16, the number of byte boundary in the structure is a multiple of the byte number sizeof (double) = 8) of the type occupying the maximum space in the structure. Therefore, no vacant byte needs to be filled. Therefore, the size of the entire structure is sizeof (str_wu) = 8 + 1 + 3 + 4 = 16. Among them, three bytes are automatically filled by VC and nothing makes sense. Str_wu1, the same principle: sizeof (char) = 1, while 1 is not a multiple of 8, so it is increased to 8, sizeofdouble) = 8, the starting address is a multiple of 16 and 16 is sizeofint. Therefore, the total number of addresses: sizeofchar) + 7 + sizeofdouble) + sizeofint) = 20, while 20 is not a multiple of 8, sizeof (double) = 8). Therefore, you need to add four addresses, that is, 24 in total. ---------------------- Sizeof is specific. All I know is what the master knows, or what errors I have written. Thank you!
This article is from the blog "drinking lemon milk tea and experiencing Green Life!