The concept and application method of C ++ sizeof

Source: Internet
Author: User

The C ++ programming language supports various programming styles and supports various functions in the C language. How should we correctly apply such a powerful programming language to program development? First, let's start with some basic knowledge, such as some concepts of C ++ sizeof introduced today.

  • Detailed description of C ++ address Operators
  • Interpretation of c ++ function transmission in three ways
  • Function implementation of C ++ clock ()
  • Explanation of the specific code application of C ++ timer
  • Application details analysis of c ++ standard library

1. What is sizeof

First, let's take a look at the definition of sizeof on msdn:

The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.

When I saw the word "return", did I think of a function? Wrong. sizeof is not a function. Have you ever passed parameters to a function without brackets? Sizeof is acceptable, so sizeof is not a function. Some people on the Internet say that sizeof is a unary operator, but I don't think so, because C ++ sizeof is more like a special macro, Which is evaluated during the compilation stage. For example:

 

 
 
  1. Cout <sizeof (int) <endl; // the length of an int on a 32-bit machine is 4.
  2. Cout <sizeof (1 = 2) <endl;
    // = The operator returns the bool type, which is equivalent to cout <sizeof (bool) <endl;

It has been translated:

 
 
  1. cout<<4<<endl;  
  2. cout<<1<<endl; 

Here is a trap. Let's look at the following program:

 
 
  1. int a = 0;  
  2. cout<<sizeof(a=3)<<endl;  
  3. cout<<a<<endl; 

Why is the output 4 or 0 instead of the expected 4 or 3 ??? It lies in the features of C ++ sizeof in the compilation phase. Because sizeof cannot be compiled into machine code, the content in the scope of sizeof, that is, (), cannot be compiled, but replaced with the type. = Operator returns the type of the left operand, so a = 3 is equivalent to int, and the code is replaced:

 
 
  1. int a = 0;  
  2. cout<<4<<endl;  
  3. cout<<a<<endl; 

Therefore, sizeof cannot support chained expressions, which is different from the unary operator.

Conclusion: Do not treat sizeof as a function, or a mona1 operator, and treat it as a special compilation preprocessing.

2. sizeof usage

C ++ sizeof has two usage methods:

1) sizeof (object)

That is, the sizeof object can also be written as a sizeof object. For example:

2) sizeof (typename)

That is to say, sizeof is used for the type. In this case, it is invalid to write sizeof typename. The following are examples:

 
 
  1. Int I = 2;
  2. Cout <sizeof (I) <endl; // sizeof (object) usage, reasonable
  3. Cout <sizeof I <endl; // sizeof object usage, reasonable
  4. Cout <sizeof 2 <endl; // 2 is parsed to an int-type object. The usage of sizeof object is reasonable.
  5. Cout <sizeof (2) <endl; // 2 is resolved to an int-type object. The usage of sizeof (object) is reasonable.
  6. Cout <sizeof (int) <endl; // sizeof (typename) usage, reasonable
  7. Cout <sizeof int <endl; // error! For operators, you must add ()

We can see that adding () is always the right choice.

Conclusion: It is best to add () regardless of the value of C ++ sizeof ().

Related Article

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.