How is sizeof implemented?

Source: Internet
Author: User

Http://blog.chinaunix.net/space.php? Uid = 20545494 & Do = Blog & id = 1929750

How is sizeof implemented? () Category: C & C ++

Sizeof, during compilation, searches for the symbol table, judges the type, and then selects the value based on the basic type. If it is struct, it is determined by the type declaration symbol table, if the string is determined by a constant table, you can refer to the symbolic table management chapter on the Compilation Principle. Generally, I will talk about it. If not, do not read this book.

I believe you have seen many general questions about sizeof.

This article discusses some uncommon details.

For more details about sizeof:

1. What is the I value after sizeof (I ++? The answer is unchanged. I remember that when I was a beginner in C language, I wanted to study the differences between sizeof and functions. The result was only Syntactic differences. after learning the assembly, I checked the code generated by the compiler, only when sizeof is compiled, it is found that a constant value is directly given, rather than the value at runtime. The sizeof (expression) results are further analyzed, and the type elevation principle is clarified. However, I did not pay attention to the side effects in expressions before, so I hesitated on the sizeof (I ++) issue. Now I have consulted materials and experiments, and concluded that sizeof is a compile-time value in most cases. Any side effects in the expression (including operators with side effects and function calls) will not happen. Here
In most cases, the exception of the Variable Length array (Variable Length array), a new feature of c99, is excluded. Refer to this article (http://rednaxelafx.javaeye.com/blog/225909), if the sizeof operator parameter is an uncertain length array, it needs to calculate the array length at runtime. 2. What is the result of sizeof ('A? It depends on whether it is in C or C ++. According to the standard, when the arithmetic type of C is increased, the character constant 'A' is automatically promoted to an integer, so the result is 4 (for 32-bit machines ); in C ++, there is a character constant. 'A' is a single-byte character constant, so the result is 1. I understand this as follows: C emphasizes the "Number" attribute of char, while C ++ emphasizes the "character" attribute of char. 3. What is the result of sizeof ('AB? I have not noticed the syntax 'AB' before. It has been found that this is called "Multi-character constant" (Multi-character constant), which is limited to 2 to 4 bytes in single quotes. According to the standard, the semantics of a Multi-byte character constant is determined by the implementation of the compiler. In GCC 4.0 and vs2008 I tested, if int A = 'abcd', A = 0x61626364. Sizeof ('AB') = sizeof ('abc') = 4. 4. What about sizeof (l'a? Although wchar_t is a wide character that can be transplanted at the source code level, its size depends on the definition of the operating system or compiler. The independent wchar_t constant does not perform arithmetic elevation like the char constant, so sizeof (L 'A') is equivalent to sizeof (wchar_t ). On 32-bit Windows and Linux, I have 2 and 4 respectively. 5. What about sizeof (L 'AB'), sizeof (L '中'), and sizeof (L 'China? It is valid to have a single character (such as '中') consisting of multiple bytes in a single quotation mark of a wide character constant, the result is equivalent to the sizeof (wchar_t) under the specific implementation ). However, multiple characters (such as l' AB 'and l'china') consisting of multiple bytes are not defined. The Compiler may report an error or provide different implementations. In gcc4.0 and vs2008 I tested, l 'abc' returns 0x64 and 0x61 respectively. Take sizeof for them, and the result is equal to sizeof (wchar_t) under the specific implementation, but note that this is standard undefined and should not be sure. In addition, we can see two exquisite macro implementations about sizeof. Non-array sizeof: # defne _ sizeof (t) (size_t) (T *) 0 + 1) array sizeof: # define array_sizeof (t) (size_t) (& t + 1)-(size_t) (& T) principle is Pointer operation in C/C ++.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.