SIZEOF misunderstood in C/C ++

Source: Internet
Author: User

The following is a detailed analysis of SIZEOF in C/C ++. For more information, see

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

Copy codeThe Code is as follows:
Int a [10];
Sizeof (a); // What is it?
Sizeof (a [10]); // What is it?
Void f (int a [10])
{
Cout <sizeof (a) <endl; // What is the value?
}
View Code


Answer:
1: for the first question, sizeof is not a function, but a built-in keyword in a language. If you do not believe it, try sizeof 4 and sizeof (int). The print result is 4. If it is a function, there must be no parentheses, so what is sizeof? :). If you still don't believe it, you can check any C ++/C programming language book, sizeof is one of the 32 keywords. If there is no sizeof, change the book :)
Why should we add parentheses for keywords? You can try sizeof int and sizeof (int). The first one can be compiled but the second one can be compiled. Think about the C/C ++ language, only signed can be added before int. unsigned, auto, const, and volative are used to modify the storage method of variables. We didn't mention sizeof. If sizeof is added, what is the storage method :)

2: sizeof is the keyword, strlen is a standard C language library function, used to obtain the string length, char * str = "abacd", sizeof str and strlen (str), compile, let's see the result. :) one result is 4, one result is 5, and the result is 4 because a pointer occupies 4 bytes, and the result is 5 because the string length abacd is exactly five characters, not explained

3: What is the value of sizeof (int) (* p? With the help of 1, it has been analyzed in detail. In fact, it is to take the value of * p, convert it to int type data, and then measure the memory bytes occupied by swap data, obviously, an int data occupies 4 bytes.

Copy codeThe Code is as follows:
Int a [10];
What is sizeof (?


What is the measurement result? The result is 40. sizeof measures the size of an array here.

Copy codeThe Code is as follows:
Int a [10];
What is sizeof (a [10?


Careful people will find that a [10] has been accessed across borders, and sizeof is used here and no error is reported, because the out-of-bounds error is a runtime exception and the compiler does not check it, in this case, the [10] compiler considers it an integer variable in the array, and the result is also 4.

Copy codeThe Code is as follows:
Void f (int a [10])
{
Cout <sizeof (a) <endl; // What is the value?
}


What is the output value above? You can write a program and try it. The result is 4. Why is it because the C/C ++ language requires that the function cannot use arrays as parameters or return values, the above functions will be converted

Copy codeThe Code is as follows:
Void f (int *)
{
Cout <sizeof (a) <endl; // What is the value?
}


Because a is a pointer to an integer and the result is 4

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.