Use three minutes to understand the C language sizeof, three minutes sizeof

Source: Internet
Author: User

Use three minutes to understand the C language sizeof, three minutes sizeof
I. Concepts

Sizeof is a single object operator, which is the same as a ++ operator. Outputs the storage size of the operation object in bytes.

Ii. Usage a. operation data type

For example, sizeof (int), the length of the output int type in the memory, depending on the specific environment, the local output is 4.

B. Operation Variables

For example, char a [6]; printf ("% d \ n", sizeof (a). Because the char array length has been defined, the output value is 6.

Iii. FAQs

Note the following examples:

Example 1:

int testSizeOf(char x[]){   return sizeof(x) ;}int main(int argc, char *argv[]){    char y[6];    int temp = testSizeOf(y);    printf("%d\n" ,temp);}
The output result here is no longer 6, because in the testSizeOf function, a pointer is actually passed in, so here sizeof actually takes the number of bytes of the pointer, so the result is 4.

Example 2:

int main(int argc, char *argv[]){    printf("%d\n" ,sizeof("0123456789"));}
Here, the output result is 11, because in the language, Ten Characters and the ending character "\ 0" are considered here ".


What is the role of the sizeof operator in C language?

It is usually used to calculate the size of a variable or type, in bytes.
For example, sizeof (char) = 1, sizeof (int *) = 4 are the size of the computing type, char is a byte, int * is 4 bytes (in fact, the pointer in 32-bit system is 4 bytes, such as char * and void)
For example, char a; then sizeof (a) = 1
Int B; then sizeof (B) = 4 (this is related to the compiler, and some are 2)

In addition, when calculating the structure and class size, you can also use sizeof to calculate the bytes occupied by the class or structure. the calculation method varies depending on the compiler, but the basic algorithms are the same. For example, char is 1 byte, int * is 4 bytes, and double is 8 bytes, double * is 4 bytes. some compilers perform alignment and some do not. however, this generally does not make much sense.

In terms of applications, such
1. Calculate the number of array elements
Int a [] = {1, 2, 4, 5, 6, 7, 8, 9, 20 };
Sizeof (a)/sizeof (a [0]) is the size of array a divided by the size of a single element. The result is the number of elements of array a, which is more convenient than the number of itself, especially when the number is large. when traversing an array, for example, for (int I = 0; I <sizeof (a)/sizeof (a [0]); ++ I ){...}
In this way, errors are not easy. Otherwise, one or more errors may occur.

2. dynamically apply for memory (when malloc, calloc, and realloc are used)
For example, to apply for a memory segment to store 30 double Data Types
You can use (double *) malloc (sizeof (double) * 30) to apply for 8*30 bytes of space and return the first address to a double * type variable.

3. When some function parameters require the number of bytes

How to Write sizeof in C Language

# Define sizeof_v (var) (size_t) (char *) (& (var) + 1)-(char *) & (var )))

# Define sizeof_t (type) (size_t) (type *) 0 + 1)

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.