Differences between sizeof and strlen

Source: Internet
Author: User

1. The result type of the sizeof operator is size_t, and typedef in the header file is of the unsigned int type. This type ensures that it can accommodate the maximum object size.

2. sizeof is an operator and strlen is a function.

3. sizeof can be a type parameter. strlen can only be a char * parameter and must end with "\ 0. Sizeof can also be used as a parameter using a function, for example:

 
  ShortF ();
Printf ("% D \ n",Sizeof(F ()));

The output result is sizeof (short), that is, 2.

4. the sizeof parameter of the array is not degraded. If it is passed to strlen, It is degraded to a pointer.

5. Most CompilationProgramSizeof is computed during compilation. It is the type or variable length. This is why sizeof (x) can be used to define the array dimension:

  Char  STR [  20  ]  =     "  0123456789  "  ;
Int A = Strlen (STR ); // A = 10
Int B = Sizeof (STR ); // B = 20

6. The strlen result can be calculated only during running, and is used to calculate the length of the string, rather than the memory size occupied by the type.

7. If sizeof is a type, brackets must be added. If it is a variable name, no brackets can be added. This is because sizeof is an operator rather than a function.

8. When a structure type or variable is used, sizeof returns the actual size. When a static space array is used, sizeof returns the size of all arrays. The sizeof operator cannot return the size of the dynamically allocated array or external array.

9. when an array is passed as a parameter to a function, it is a pointer rather than an array, and the first address of the array is passed, such as fun (char [8]) and fun (char []). it is equivalent to fun (char *). Passing an array in C ++ is always a pointer to the first element of the array. The Compiler does not know the size of the array. If you want to know the size of the array in the function, you need to do this: after entering the function, use memcpy to copy the array, and the length is transmitted by another parameter.CodeAs follows:

  Fun (unsigned  Char     *  P1,  Int  Len)
{
Unsigned Char * Buf = New Unsigned Char [Len + 1 ];
Memcpy (BUF, P1, Len );
}

10. to calculate the size of the structure variable, we must discuss the Data Alignment issue. To make CPU Access faster, C ++ often calculates the size of the members in the Structure Variable in multiples of 4 or 8 when processing data, this is called data alignment ). This may waste some memory, but theoretically the CPU speed is faster. Of course, this setting will cause inconvenience when reading and writing data files generated by other applications or exchanging data. Alignment settings in ms vc ++, sometimes sizeof gets different from the actual. Generally, add the # pragma pack (n) Setting in VC ++. Alternatively, if you want to store data by byte without Data Alignment, you can modify "Data Alignment" on the advanced compiler tab in the Options dialog box to align data by byte.

11. the sizeof operator cannot be used for function type, incomplete type, or bit fields. Data Types with location-based data storage sizes, such as arrays with unknown storage sizes, structures with unknown content, Union types, and void types.

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.