Introduction and comparison of sizeof of and strlen of functions in C language

  • 2020-05-07 20:06:01
  • OfStack

sizeof () function
1, what is it?
      sizeof is really just one operator, and those +,-1 things, and they're parsed at compile time. Although we often see sizeof followed by a parenthesis that looks like a function, it's not a function at all.
2. What's the use?
      sizeof is basically telling us how much space the compiler is going to carve out in bytes for a particular piece of data or a particular type of data.
3. How?
      sizeof(type), or sizeof(variable), all you get is the storage space of the type or variable. When using a variable, you can also use the sizeof variable without parentheses, but not 1. .
4. What should I pay attention to when using it?
(1) the size of the occupied space returned by sizeof is the size allocated for this variable, not just the space it USES. Similar to the concept of building area and usable area of today's housing. So most of the time when you're using structures, you have to think about byte alignment.
(2) things that the compiler can't even determine the storage space, such as field members, cannot be used. This should make sense, because sizeof returns data in bytes, and you ask it to find the size in bits. So the compiler's solution is to not accept all 1, even if you say you happen to be 8 bits, 1 byte, the compiler will ignore you.
(3) the data type returned by sizeof is unsigned int. Because of the automatic conversion of different types of data mixing operation in C, sometimes it may cause problems if you do not pay attention to it. For details, please refer to the following program example.
(4) notice the difference between array names and pointer variables. In general, we always think that the array name is similar to the pointer variable, but when using sizeof, it is quite different. When using sizeof for the array name, it returns the size of the entire array, while when operating on the pointer variable, it returns the space occupied by the pointer variable itself. And when the array name is a function parameter, inside the function, the parameter is a pointer, so the size of the array is no longer returned.
5. Case analysis
Source:


#include<stdio.h>
 
int main()
{
    int iVal = 3;
    printf("The size of type int is %d \n", sizeof(int));
    printf("The size of iVal is %d\n", sizeof(iVal));
    printf("The size of iVal is %d\n", sizeof iVal);
 
    if((iVal - sizeof(int)) < 0)
    {
        printf("The return type is int\n");
    }
    else
    {
        printf("The return type is unsigned int\n");
    }
 
    char chArrayCon[7];
    char *chp;
    chp = chArrayCon;
    printf("The size of chArrayCon is %d, The size of chp is %d\n", sizeof(chArrayCon), sizeof(chp));
 
    int iArraySize = 3;
    char chArrayVar[iArraySize];
    printf("The size of chArrayVar is %d\n", sizeof(chArrayVar));
    return 0;
}

Operation results:


The size of type int is 4 
The size of iVal is 4
The size of iVal is 4
The return type is unsigned int
The size of chArrayCon is 7, The size of chp is 4
The size of chArrayVar is 3

Result analysis:
Part 1 of the       code simply shows the usage of 1.
Part 2 of       explains item 3 of the DOS and don 'ts. iVal is supposed to be 3, minus 4 should be minus 1, less than 0, and the output should be "he return type is int". But since when int and unsigned int1 start, it will default to unsigned int, the result will be a large unsigned int number, which is greater than 0.
Part 3 of the       code simply explains the difference between an array name and a pointer variable. Even if you point a pointer variable to an array name, the compiler can still tell the difference.

      above does not consider the situation under C99 standard. Because there is a special case under the C99 standard, which is the use of indeterminate arrays. When sizeof is used on an indeterminate array name, it also returns the size of the entire array, just like block 4 code 1 in the example. But this is not done at compile time, it is done at run time. Because at compile time, compiler 1 generally does not know what the value of the variable is. (of course, in the example I assigned 3 directly, which may not be easy to understand, you can assume that iArraySize gets the actual value through scanf, and then declares the array). For this kind of situation, the actual application is not much, you can treat it as a special case, or just ignore it...
6 summarizes
      actually sizeof is to tell us the size of the "floor area" assigned to the variable, as long as you remember this point should be enough, whether the variable type is ordinary integer data, or structure, common body, enumeration... With that in mind, when we want to know what values sizeof returns for different data types, we just need to figure out how much "floor space" those data types actually consume.

strlen () function
The
C library function size_t strlen(const char *str) calculates the length of the string str, but does not include the terminating null character.

The statement
Here is the declared strlen() function.


size_t strlen(const char *str)

parameter
str -- this is the length of the string to calculate.

The return value
This function returns the length of the string.

example
The following example shows the use of the strlen() function.


#include <stdio.h>
#include <string.h>

int main ()
{
  char str[50];
  int len;

  strcpy(str, "This is yiibai.com");

  len = strlen(str);
  printf("Length of |%s| is |%d|
", str, len);
  
  return(0);
}

Let's compile and run the above program, which will produce the following results:


Length of |This is yiibai.com| is |26|


sizeof differs from strlen in usage
The result type of the
1.sizeof operator is size_t, which is of type 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 (C++ keyword) and strlen is the function.

3. sizeof can use type as parameter, strlen can only use char* as parameter, and must end with "\0". sizeof can also use functions as parameters, such as:


short f();
printf("%d\n", sizeof(f()));

The output is the size of the type of the return value, sizeof(short)=2.

4. The array does not degrade the parameters of sizeof, and if passed to strlen, it will degenerate into a pointer. Most compilers calculate sizeof at compile time, which is the type or the length of a variable, 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); //b=20;

The result of strlen cannot be calculated until it is run. It is used to calculate the length of the string, not the size of the type in memory.

5. After sizeof, if it is a type, it must be bracketed; if it is a variable name, it can be unbracketed. This is because sizeof is an operator and not a function.

6. When applied to a structure type or variable, sizeof returns the actual size. When applied to a statically spaced array, sizeof gets the size of the entire array. The sizeof operator cannot return the size of a dynamically dispatched array or an external array.

7. When the array is passed to the function as an argument, the pointer is passed instead of the array, and the first address of the array is passed, such as:


fun(char [8])
fun(char [])

This is the same thing as fun times char.

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:

Enter the function and copy it with memcpy. 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);
}

sizeof on the pointer, the result is the corresponding type:


char* ss = "0123456789";
sizeof(ss)

So it's going to be 4 is equal to 1 > ss is a character pointer to a string constant. sizeof obtains the space occupied by one pointer, which should be a long integer, so it is 4. sizeof(*ss) result 1, = > *ss is the first character, which is essentially the memory space taken up by the first bit "0" of the string, which is of type char and takes up 1 byte, strlen(ss)= 10 > > > > If you want to get the length of the string, 1 must use strlen.


Related articles: