Usage of sizeof

Source: Internet
Author: User

sizeof usage

Let's start by stating that sizeof is a C-language operator that executes at compile time, just like normal + 、-、 *.

sizeof has two grammatical forms: 1, sizeof (object) or sizeof object 2, sizeof (type) (can be abstract type or generic type)

As an example, I understand that int i. You can use sizeof I or sizeof (i) to find out that I is the object of the space occupied by the number of bytes, obviously 4;sizeof (int), the size of this object type is calculated. For example, a char is 1 bytes.

sizeof has several common error usage, namely: 1, the size of the array, 2, the pointer size 3, the function using 4. Find the structure size. 5. Use the following answers to the common body Union

One, the size of the array, the size of the array you define the time of the declaration, whether you run out of space or not.

Here's an example: Char a[100]= ' 1281 '. Then sizeof (a) was evaluated to be 100. Instead of the 4 you used strlen. (Strlen encounters '/0 ' on the end count, and strlen can only find the size of the string)

Second, to seek the size of the pointer. This is the content of your pointer in the end of a few bytes, then the number of bits of your computer, you are 32 is 4 bytes, you are 64 bits is 8 bytes. such as Char *p= ' 1281 '; then sizeof (p) for 32 is 4, 64 bits is 8.sizeof (*p) is 1, is quite seeking sizeof (p[0]).

Third, the use of the function. Char fool (char p[100]) {sizeof (P)}; This result is P equivalent to a pointer. 4 or 8.

Sizeof (Fool (' 1281 ')) result is 1 because the return type is char

Four, the structure of the use of more complex.

Before the explanation, we should discuss one problem, that is, the structure boundary alignment problem. Alignment facilitates the reading speed of the computer. Alignment satisfies three principles 1) the first address of a struct type can be divisible by its widest base type member size 2) The first address of each base type in the struct must be an integer multiple of the size of the type, and if there is a need to populate the members with Byte 3, the entire structure must be an integer multiple of its widest base type member.

An example would be the best illustration of a problem.

Let's look at another scenario:

Usage of sizeof

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.