Based on the difference between Sizeof and Strlen and the usage of the association

  • 2020-04-01 21:38:26
  • OfStack

A, sizeof
      Sizeof (...). Is an operator, in the header file typedef is unsigned int, its value is calculated at compile time, the parameters can be array, pointer, type, object, function, etc.
      Its function is to obtain the byte size that is guaranteed to hold the largest object created by the implementation.
      Since it is calculated at compile time, sizeof cannot be used to return the sizeof dynamically allocated memory space. In fact, sizeof is used to return the space taken by the type and statically allocated objects, structures, or arrays, regardless of what the object, structure, or array is stored in.
      Specifically, when the parameters are as follows, the value returned by sizeof means the following:
      Array - the size of the array space allocated at compile time;
      Pointer -- the size of the space used to store the pointer (the length of the address used to store the pointer, which is a long integer, should be 4);
      Type -- the amount of space occupied by the type;
      Object - the actual size of the space occupied by the object;
      Function - the amount of space taken up by the return type of the function. The return type of a function cannot be void.
* * * * * * * * * * * * * *

Second, the strlen
      Strlen (...). It's a function, it's only evaluated at run time. The argument must be a character pointer (char*). When the array name is passed in as an argument, the array is actually reduced to a pointer.
      It returns the length of a string. The string may be self-defined or random in memory, and what the function actually does is iterate from the first address that represents the string until the NULL terminator is encountered. The length size returned does not include NULL.
* * * * * * * * * * * * * * * * *

Iii. Examples:
      Eg1, char arr[10] = "What?" ;
                          Int len_one = strlen (arr);
                          Int len_two = sizeof (arr);
                          Cout < < Len_one < < "And" < < Len_two < < Endl;
      The output is: 5 and 10
      Comment: sizeof returns the sizeof the arr array that the compiler assigns to it when it defines the arr array, regardless of how much data is stored in it. Strlen only CARES about the content of the data stored, not the size or type of space.

      Eg2, char * parr = new char[10];
                          Int len_one = strlen (parr);
                          Int len_two = sizeof (parr);
                          Parr int len_three = sizeof (*);
                          Cout < < Len_one < < "And" < < Len_two < < "And" < < Len_three < < Endl;
      Output: 23 and 4 and 1
      Comments: the first output result 23 May actually be different from run to run, depending on what is stored in the parr (from parr[0] until the first NULL end is encountered); The second result was actually intended to calculate the sizeof the dynamic memory space pointed to by parr, but instead sizeof considered parr to be a character pointer and returned the space occupied by the pointer (the pointer is stored as a long integer, so it is 4). The third result, since *parr represents the characters in the address space referred to by parr, the length is 1.
* * * * * * * * * * * *

Iv. References
The difference and connection between Sizeof and Strlen

1. The result type of sizeof operator is size_t, which is typedef as unsigned int in the header file.
This type is guaranteed to hold the byte size of the largest object created by the implementation.

2. Sizeof is the operator and strlen is the function.

Sizeof can be used as an argument, strlen can only use char* as an argument, and must end with ''\0'.
Sizeof can also use functions as arguments, such as:
Short (f);
Printf (" % d \ n ", sizeof (f ()));
The output is sizeof(short), which is 2.

4. If the array does not degenerate into sizeof, it will degenerate into pointer if passed to strlen.

Most compilers calculate sizeof to be the type or the length of a variable at compile time which is why sizeof(x) can be used to define array dimensions
Char STR [20] = "0123456789";
Int a = strlen (STR); / / a = 10;
Int b = sizeof (STR); / / and b = 20;

6. Strlen's results are calculated at run time to calculate the length of the string, not the size of the type in memory.

If it is a type, it must be enclosed; if it is a variable name, it can be unenclosed. This is because sizeof is an operator and not a function.

8. Sizeof returns the actual size when applied to a structure type or variable,
When a static space array is applied, sizeof returns the sizeof the entire array.
The sizeof operator cannot return the sizeof a dynamically dispatched array or an external array

9. When an array is passed to a function as an argument, it passes a pointer instead of an array, and the first address of the array is passed.
Such as:
Fun (char [8])
Fun (char [])
This is the same thing as fun of char star.
In C++ the argument passing array is always passing 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, it is copied out with memcpy, and the length is passed in by another parameter
Fun (unsiged char *p1, int len)
Unsigned char* buf = new unsigned char[len+1]
Memcpy (buf, p1, len);

We can always use sizeof and strlen to calculate the length of an array of strings
After reading the detailed explanation above, it is found that there is still a difference in the use of the two. From this example, we can see clearly:

Char STR [20] = "0123456789";
Int a = strlen (STR); / / a = 10; > > > > Strlen calculates the length of the string, ending with the terminator 0x00 for the string.
Int b = sizeof (STR); / / and b = 20; > > > > Sizeof calculates the sizeof the allocated array STR [20], regardless of what is stored in it.

The above is the result of the static array processing, if the pointer, the result is different

Char * ss = "0123456789";
Sizeof (ss) result 4 = = = > ss is a character pointer to a string constant, sizeof gets the space of a pointer, should be

Long integer, so it's 4
Sizeof (*ss) result 1 = = = *ss is the first character that gets the memory space of the first '0' of the string, which is the char class

Type one, occupied 1 place

Strlen (ss) = 10 > > > > If you want to get the length of the string, be sure to use strlen

Related articles: