Summary of SizeOf operators: First look at some other people's introduction summary:
1 .FROM: << C Primary 3rd Edition >>
The action of the SISEOF operator is to return an object or type name of the byte length it has the following three forms; sizeof (object); sizeof (); SizeOf Object; the type of return value is Size_T This is a machine related Typedef Defines We can find its definition in the cstddef header file. The following example uses sizeof's two formats #include
: 2Short [3]: 6 As the example above, the SIZEOF operator applied to the pointer type is returned to the memory length required to include the memory length required to include the type of address but the application on the reference type is returned. The SIZEOF operator required to include the reference object is applied to all C implementation results when the CHAR type is 1 // is guaranteed to be 1Size_t char_size = sizeof (char) in all implementations; Sizeof operator is compiled The time calculation is therefore considered to be a constant expression that can be used in any way in any of the required constant expressions, such as arrays or templates, for example // ok: compile time constant expressions int Array [SIZEOF (SOME_TYPE_T)] ; 2. From netizen.
Analytical sizeof http://www.ccidnet.com of C language: Min Fang, Wu Mingming (2001-04-19 14:43:36) First, the concept sizeof sizeof is a C language unary operator, For example, other operators in C are , - etc. It is not a function. The SIZEOF operator gives the storage size of its operand in byte. The number of operands can be an expression or a type name enclosed in parentheses. The storage size of the operand is determined by the type of operand.
Second, the use of SIZEOF 1, used for data types
SIZEOF use form: sizeof (Type)
The data type must be enclosed in parentheses. Such as SIZEOF (int).
2, used for variables
SizeOf use form: sizeof (var_name) or sizeof var_name
The variable name can be horn without parentheses. Such as sizeof (var_name), sizeof var_name, etc. are all correct forms. Usage of brackets is more common, most programmers use this form.
Note: The SIZEOF operator cannot be used for function types, incomplete types or bit fields. Incomplete type refers to a data type with unknown storage size, such as an unknown storage size, an unknown content of the content or a combination type, a Void type, and the like.
If SIZEOF (MAX), if the variable max is defined as int max (), sizeof (char_v) If CHAR_V is defined as char char_v [max] and Max is unknown, SizeOf (Void) is not correct.
Third, the result type of SIZEOF operator is Size_T, which in the header file is typedef for the unsigned int type. This type ensures that the byte size of the maximum object established can be accommodated.
1. If the operand has a type char, unsigned char or signed char, the result is equal to 1.
ANSI C officially specifies that the character type is 1 byte.
2, int, unsigned int, unsigned long, flos 2, 4, 4, 4, 8, 10.
3. When the operand is a pointer, SIZEOF depends on the compiler. For example, in Microsoft C / C 7.0, the NEAR class norm is 2, FAR, and the HUGE class pointer is 4. General Unix's pointer one is 4.
4. When the operand has an array type, the result is the total number of bytes of arrays.
5. SIZEOF of the joint type operand is the number of bytes of its maximum byte member. SIZEOF of the structure type operand is the total number of bytes of this type of object, including any pad.
Let us see the following structure:
Struct {char b; double x;} a;
SIZEOF (A) = 12 on some machines, and general SizeOf (char) sizeof (double) = 9. This is because the compiler is inserted into the space in the structure in the structure to control the address alignment of each member object when considering alignment problems. If the Double type of structural member X is to be put on the address of 4.
6. If the operand is the shape parameter or function type in the function, SIZEOF gives the size of its pointer.
Fourth, the relationship between SIZEOF and other operators SIZEOF has a priority of Level 2, ratio /,% and other 3 operators. It can form an expression with other operators. Such as i * sizeof (int); where i is an int type variable.
5. SizeOf's main purpose 1, one of the SizeOf operators is to communicate with routines such as storage allocation and I / O systems. E.g:
Void * malloc (size_t size),
SIZE_T FREAD (Void * PTR, SIZE_T SIZE, SIZE_T NMEMB, FILE * stream).
2, another main purpose of SizeOf is to calculate the number of elements in arrays. E.g:
Void * MEMSET (Void * S, INT C, SIZEOF (S)).
6. It is recommended that due to the number of bytes of operands may have changed, it is recommended to use ZiseOf to replace constant calculations in ZISEOF when it involves the operating number. (http://www.fanqiang.com) 3. From smth C version
Sender: jiangfei (Afei), the letter area: CPLUSPLUS Title: About Sizeof's small problem, Thank you for the enlightenment station: BBS Shuimu Tsinghua Station (Tue Mar 15 15:08:06 2005),
#include
Int main () {
Char str [] = "a short string"; string st = "a short string"; cout << sizeof (str) // 15, calculation '/ 0' << endl << sizeof (st) // 16, why << Endl; if (STR [14] == '/ 0') cout << "OK1" << Endl; // ok1.i c. If (ST [15] == '/ 0') COUT < <"ok2" << Endl; // ok2, why?
}
-
※ Source: · BBS Shuimu Tsinghua Station http://smth.org- [from: 202.118.229. *]
[Benpian full text] [Reply to this article] [reply to authors] [Benpian Author: xxxdg] [into the forum] [Back to top] 2 Sender: xxxdg (desert lizards), the letter area: CPlusPlus Title: Re: About Sizeof's small problem, Thank you for the enlightenment station: BBS Shuimu Tsinghua Station (Tue Mar 15 15:15:18 2005),
Do you want to try string st = "hello"; cout << sizeof (st) << Endl;
[In the masterpiece of JIANGFEI (Afei): 】inClude
※ Source: · BBS Shuimu Tsinghua Station Smth.org · [from: 162.105.77. *]
[Benpian full text] [Reply to this article] [reply to authors] [Benpian Author: jiangfei] [into the forum] [Back to top] 3 Sender: jiangfei (afei), the letter area: CPlusPlus Title: Re: About sizeof Small problem, thank you to enlighten meal: BBS Shuimu Tsinghua Station (Tue Mar 15 15:24:54 2005),
Blush! I laugh! But why ??? all area????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
[In the masterpiece of xxxdg (desert lizard):]: Do you want to try: string st = "hello" ;: COUT << SIZEOF (ST) << Endl ;: ......... ........
-
※ Modification: · JIANGFEI in Mar 15 15:27:10 2005 Modified this article · [From: 202.118.229. *] ※ Source: · BBS Shuimu Tsinghua Station http://smth.org· [from: 202.118.229. * ]
[Benpian full text] [Reply to this article] [reply to authors] [Benpian Author: jiangfei] [into the forum] [Back to top] 4 Sender: jiangfei (afei), the letter area: CPlusPlus Title: Re: About sizeof Small problem, thank you to enlightenment station: BBS Shuimu Tsinghua Station (Tue Mar 15 15:30:53 2005),
The sizeof operator cannot Return The size of dynamically allocated arrays or external arrays.
But return 16 for sizeof (string) Stands for nothing at all? [Mentioned in the masterpiece of jiangfei (Afei):]: blush !: Laughing! : But why ???: ...................
-
※ Source: · BBS Shuimu Tsinghua Station http://smth.org- [from: 202.118.229. *]
[Benpian full text] [Reply to this article] [reply to authors] [Benpian Author: xxxdg] [into the forum] [Back to top] 5 Sender: xxxdg (desert lizards), the letter area: CPlusPlus Title: Re: About SizeOf's small problem, thank you to the enlightenment station: BBS Shuimu Tsinghua Station (Tue Mar 15 15:33:44 2005),
Think and answer my question, the you will got itclass a {public: a () {_data = new int [100];} ~ a () {delete [] _DATA;} int * _data;};
a _x; cout << sizeof (_X) << endl;
Output Choice: 1) 1002) 1283) 4 4) 4 SizeOf (a and ~ a)
[In the masterpiece of jiangfei (Afei):;]: Blush !: Laughing! : But why ???: All Are 16?
-
※ Source: · BBS Shuimu Tsinghua Station Smth.org · [from: 162.105.77. *]
[Benpian full text] [Reply to this article] [reply to authors] [Benpian Author: jiangfei] [into the forum] [Back to top] 6 Sender: jiangfei (afei), the letter area: CPlusPlus Title: Re: About sizeof Small problems, thank you to enlighten meal: BBS Shuimu Tsinghua Station (Tue Mar 15 15:36:25 2005),
but why
String st = "a short string"; ST [15] == '/ 0'? [mentioned in the masterpiece of xxxdg (desert lizard):]: Do you want to try: string st = "hello" ;: COUT << sizeof (st) << Endl ;: .................
-
※ Source: · BBS Shuimu Tsinghua Station http://smth.org- [from: 202.118.229. *]
[Benpian full text] [Reply to this article] [reply to authors] [Benpian Author: xxxdg] [into the forum] [Back to top] 7 Sender: xxxdg (desert lizards), the letter area: CPlusPlus Title: Re: About Sizeof's small problem, Thank you for the enlightenment station: BBS Shuimu Tsinghua Station (Tue Mar 15 15:37:40 2005),
CAN operator [] be overloaded?
[In the masterpiece of jiangfei (Afei):]: But why: string st = "a short string" ;: ST [15] == '/ 0' ??
-
※ Source: · BBS Shuimu Tsinghua Station Smth.org · [from: 162.105.77. *]
[Benpian full text] [Reply to this article] [reply to authors] [Benpian Author: owl2008] [into the forum] [Back to top] 8 Sender: owl2008 (owl), the letter area: CPlusPlus Title: Re: About sizeof Small problems, thank you to enlighten meal: BBS Shuimu Tsinghua Station (Tue Mar 15 15:38:27 2005),
type object; sizeof object == sizeof type mentioned in [jiangfei (afei) role: -]: The sizeof operator can not return the size of dynamically allocated arrays or external arrays .: But return 16 for sizeof (string) stands for nothing AT all?
- ID OWLMSN: Lingjiexyz@hotmail.comqq: 85475939blog: http://blog.9cbs.net/owl2008/
※ Source: · BBS Shuimu Tsinghua Station Smth.org · [From: 220.184.16. *] Next, sum up my own understanding:
<1>. Alignment problem is very subtle: <2>. An important purpose: Since the number of operands may change during the implementation. It is recommended to replace constant calculations in ZiseOf when it involves an operational digital size. INT IA [] = {0, 1, 2}; // SizeOf Returns the size size_t array_size = sizeof ia; // sizeOf Returns the size size_t element_size = array_size / sizeof (int); <3>. When the operand is a pointer, the SIZEOF relies on the compiler and is the size of the pointer and the size of the pointer is pointed. In the existing 32-bit machine, the SIZEOF (Pointer) is 4. <4>. When the operand has an array type, its result is the total number of bytes of arrays. <5>. Size Size has nothing to do with the length of the string it refers to. SIZEOF (string str = "hello"); sizeof (String str = "a long long long long position)) The environment (32-bit machine, VC6.0 compiler) is 16. <6>. SizeOf of class becomes delicate due to alignment, especially when there is virtual function, it is more difficult to touched. For example, Class A {public: a () {_data = new char [100];} ~ a () {delete [] _data;} char * _DATA; // sizeof (POINTER) = 4 long data2; // sizeof (long) = 4 char dat [10]; // sizeof (dat) = 10 char dat1; // sizeof (char) = 1}; a _x; cout << sizeof (_X) << Endl; // output 20 becomes: Class a {public: a () {_data = new char [100];} ~ a () {Delete [] _Data;} char * _DATA; // Sizeof (Pointer) = 4 char dat [10]; // sizeof (dat) = 10 change position long data2; // sizeof (long) = 4 change position char DAT1 ; // sizeof (char) = 1}; a _x; cout << sizeof (_X) << endl; // output 24