An introduction to the three-point analysis of sizeof details in C language

Source: Internet
Author: User
The following is a detailed analysis of the details of sizeof in C language, the need for friends can refer to the next

1.sizeof is the operator, which is essentially the same as the nature of the subtraction, executed at compile time, not at runtime.
So what if this is validated in programming? PS: This is the first two days of friends Taobao interview a problem , small series Understand:

Copy Code code as follows:


#include <iostream>





using namespace std;





int main ()


{


int i=1;


cout<<i<<endl;


sizeof (++i);


cout<<i<<endl;


return 1;


}


Enter a result of 1
1
The side effects of the ++i in the sizeof are not shown, because only one may be, at compile time sizeof execution will be ++i processed, and ++i side effects are eliminated. If the sizeof is carried out at run time, be sure to pay attention to ++i. In fact, the implementation of sizeof should be done with macros, and macros are executed at compile time. Specific implementation can refer to the following.

2.sizeof (' a ') in C language result is 4, in C + + results are 1, read an article said C sizeof focus on "number", and C + + sizeof more emphasis on "character."

3. The article is about two classic applications that use macros to realize sizeof.

Copy Code code as follows:


//Suitable for non-array


#define _SIZEOF (T) (size_t) ((t*) 0 + 1)


//Suitable for array


#define ARRAY_SIZEOF (T) (size_t) (&t+1)-(size_t) (&t))


First, two small examples illustrate the application of two macros, for the first such as _sizeof (int); The result is 4; for the second, declare an array int a[4 with a size of 4, and array_sizeof (a) The result is 16.

For a non-array macro definition, first converts 0 to the address that the pointer to the t* type points to (at this point the address is 0). Then add 1 to the address of type T, which is equivalent to the size of the T type (that is, the size of the non-array t). The previous size_t only returns an integer that converts the address to an int type.

A simple example: int* p; p=p+1; --------P is a pointer to a int* type, p+1 the equivalent of 4 bytes in the address space.

For a macro definition of an array, similar to a non-array macro definition, for ease of understanding, array T can be considered a user-defined type, &t a pointer to an array type, and an array type pointer plus 1 equals the size of the array on the address. Because it is a user-defined type, you cannot force 0 to be converted to an array type of address, and you can only subtract the previous address with an additional 1 address, and the difference is the size of the byte that the array itself occupies.

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.