The misunderstood sizeof in C + +

Source: Internet
Author: User
Tags strlen
The following is a detailed analysis of the sizeof in C/s + +, the need for friends under the reference

Is 1:sizeof a function?
What is the difference between 2:sizeof and strlen?
What is the value of 3:sizeof (int) (*P)?

Copy Code code as follows:


int a[10];


sizeof (a);//How much is it?


sizeof (A[10])/How much is it?


void f (int a[10])


   {


cout<<sizeof (a) What is the <<endl;//value?


   }


View Code


Answer:
1: For the first question, sizeof is not a function, but a language built-in keywords, do not believe you try sizeof 4 and sizeof (int), printing results are 4, if it is a function, you must add parentheses, that is, then there is no parenthesis, then sizeof is what it:), If you do not believe, you can find any C++/C program language book check, sizeof is one of 32 keywords, if not, you change a book it:
That is, why do I have to add parentheses to the keyword? You can try it. sizeof int and sizeof (int), the first one will compile pass, and the second can be compiled, think of C + + language requirements, int before can only add signed.unsigned,auto,const,volative, Used to modify the storage method of variables, can not mention the front can add sizeof yo, if the front plus sizeof means what storage mode it:

2:sizeof is the keyword, strlen is a standard C language library function, used to find the length of the string, char *str= "ABACD", sizeof str and strlen (str), compile, and see the result is known:), one result is 4, one result is 5 , the result is 4 because one pointer occupies 4 bytes, and the result is 5 because the string length ABACD exactly 5 characters and does not explain

What is the value of 3:sizeof (int) (*P)? This is the use of 1, has been analyzed in detail, in fact, is to take the value of the *p, to convert it to the type int, and then measure the number of bytes of memory of the 譔 data, apparently an int data for 4 bytes

Copy Code code as follows:


int a[10];


sizeof (a) What is the number?


How much is the measurement result? The result is that 40,sizeof is measuring the size of an array here.

Copy Code code as follows:


int a[10];

how much is
sizeof (a[10])?


Careful people will find that a[10] has been crossing the border, where the use of sizeof does not error, because the error is a Run-time exception, the compiler does not check, at this time a[10] compiler that is the array of an integer variable, the result is certainly 4

Copy Code code as follows:


void f (int a[10])


   {


cout<<sizeof (a) What is the <<endl;//value?


   }


What is the value of the above output? You can write a program to try, the result is 4, why, because the C + + language specified function can not be an array as parameters or return values, the above function in the actual compilation process will be converted into

Copy Code code as follows:


void f (int *a)


   {


cout<<sizeof (a) What is the <<endl;//value?


   }


Because a is a pointer to the shape of the plastic, the result is 4 and it's obvious

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.