The difference between strlen and sizeof

Source: Internet
Author: User

Voidngx_time_init (void) {    Ngx_cached_err_log_time.len = sizeof ("1970/09/28 12:00:00")-1;    Ngx_cached_http_time.len = sizeof ("Mon, Sep 1970 06:00:00 GMT")-1;    Ngx_cached_http_log_time.len = sizeof ("28/sep/1970:12:00:00 +0600")-1;    Ngx_cached_http_log_iso8601.len = sizeof ("1970-09-28t12:00:00+06:00")-1;    Ngx_cached_syslog_time.len = sizeof ("Sep 12:00:00")-1;    Ngx_cached_time = &cached_time[0];    Ngx_time_update ();}

Read the Ngx_time_init in Nginx found that the operation of the constant string is the use of sizeof operation and then 1 operation, visible nginx for performance pursuit and details of the perfect. sizeof is an operator and not a function, so it can be obtained directly during compilation, and Strlen is required for the function to be available in the run.

Attached: the difference between sizeof and strlen:

First, sizeof
sizeof (...) is an operator, the typedef in the header file is Unsignedint, its value is calculated at compile time, the parameters can be arrays, pointers, types, objects, functions and so on.
Its function is to ensure that the size of the byte that implements the largest object being built is guaranteed to be accommodated.
Because it is evaluated at compile time, sizeof cannot be used to return the size of the dynamically allocated memory space. In fact, using sizeof to return a type and the space occupied by statically allocated objects, structures, or arrays, the return value is not related to what the object, structure, or array stores.
Specifically, when the parameters are as follows, the value returned by sizeof represents the following meanings:
Array-the size of the array space allocated at compile time;
Pointer-The amount of space used to store the pointer (the length of the address where the pointer is stored is a long integer, which should be 4);
Type-the amount of space that the type occupies;
Object-The actual amount of space occupied by the object;
Function--The amount of space that the return type of the function occupies. The return type of the function cannot be void.
Second, strlen
Strlen (...) is a function that can be evaluated at run time. The argument must be a character-type pointer (char*). When an array name is passed in as a parameter, the array is actually degenerate into a pointer.
Its function is: Returns the length of the string. The string may be self-defined or random in memory, and the function actually completes the function of iterating from the first address representing the string until the Terminator is encountered null. The returned length size does not include NULL.

The difference between strlen and 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.