Detailed explanation of the details and usage of sizeof() and strlen()

Comparison of sizeof and strlen

  1. sizeof() Calculate the size of the memory space occupied by variables,Unit is byte, if the operand is of type, the calculation isThe size of the memory space occupied by variables created using the type
    sizeof ()Only pay attention to the size of the memory space occupied, not what data is stored in the memory
  2. strlen()It is a C language library function whose function is to find the length of a string. Function prototype:
  size_t strlen ( const char * str );

The statistics are the number of characters in the previous string starting from the address in strlen()the parameter of the function . The function will keep looking for characters until it is found, so there may be an out-of-bounds search.str\0 strlen()\0
Then let's think about strlen()what will happen if we pass a character to? Please see below for details strlen(*arr), we will talk about actual cases at this time! ! !
Then let’s think about strlen()what happens if we pass an array address to? Please see below for details strlen(&arr), we will talk about actual cases at this time! ! ! Insert image description here
There is another very important point before starting the following introduction,The meaning of array names:
3. sizeof(array name), the array name here represents the entire array,Calculates the size of the entire array
4. &array name, the array name here represents the entire array,What is taken out is the address of the entire array
5. In addition, all array names represent the address of the first element.

Compute one-dimensional array

integer array

    int a[] = {
    
    1,2,3,4};
    printf("%d\n",sizeof(a));
    printf("%d\n",sizeof(a+0));
    printf("%d\n",sizeof(*a));
    printf("%d\n",sizeof(a+1));
    printf("%d\n",sizeof(a[1]));
    printf("%d\n",sizeof(&a));
    printf("%d\n",sizeof(*&a));
    printf("%d\n",sizeof(&a+1));
    printf("%d\n",sizeof(&a[0]));
    printf("%d\n",sizeof(&a[0]+1));

Let’s take a look at the output results first:
Insert image description here
1. sizeof(a);//sizeof(array name) calculates the total size of the array -= unit bytes-16
2. sizeof(a+0);//Someone must want to ask if the previous one was still 16 bytes, why does it become 8 here? In fact, this a+0is an expression that does not satisfy the form of sizeof (array name), so here ais still the address of the first element, a+0and the address of the first element, and the address size is4 / 8Byte
3. sizeof(*a);//Here ais the address of the first element, so *ait is the size of the type ( int) of the first element, which is4
4. sizeof(a+1);//Here ais the address of the first element, which a+1is the address of the second element. Analogous to 2., it is4 / 8Bytes
5. sizeof(a[1]);//The size of the second element,4
6. sizeof(&a);// &aWhat is taken out is the address of the array, all addresses are4 / 8
7. sizeof(*&a);// It is the address of &athe array . After dereference, it is the array . The size of the array is calculated here.a*a16Byte
8... sizeof(&a+1);//Based on the above, it is not difficult to imagine that this &a+1is equivalent to &askipping an array, but it is still an address.4 / 8Byte
9... sizeof(&a[0]);//First element address,4 / 8Byte
10... sizeof(&a[0]+1);//The address of the second element,4 / 8byte

character array

The usage of character arrays and integer arrays sizeof()are very similar, so I won’t introduce them in detail here!

    char arr[] = {
    
     'a','b','c','d','e','f' };
    printf("%d\n", strlen(arr));
    printf("%d\n", strlen(arr+0));
    printf("%d\n", strlen(*arr));
    printf("%d\n", strlen(arr[1]));
    printf("%d\n", strlen(&arr));
    printf("%d\n", strlen(&arr+1));
    printf("%d\n", strlen(&arr[0]+1));

Running results:
Insert image description here
1. strlen(arr);
2.strlen(arr+0);

We can see that this character array does not '\0'end with, so strlen()when calculating the address of the first element, a random value is generated! So the first and second are random values!
3. strlen(*arr);
4. strlen(arr[1]);
At the beginning we introduced that strlen()the function receives an address. Here *arris the first element of the array arr[1]and the second element of the array. So the compiler will report an error. We can also go into more detail. Since what strlen()is received is an address, *arrbut a character a, ASCIIthe value of the table is 97. At this time, strlen()97 will be regarded as an address to access the space, but this address is not the space we own, which forms illegal access.
Insert image description here
This 0x0000000000000061is actually 97, which confirms our statement.
5. strlen(&arr);
6. strlen(&arr+1);
7. strlen(&arr[0]+1);
&arr, &arr+1, &arr[0]+1are actually addresses. Although the first one is the address of an array, its address is the same as the address of the first element, so it israndom value, the second one skips an array so isRandom value -6, 6 is the array size, and the third one thinks that one element in the array has been skipped, so it isRandom value -1.
It can be seen that although &arrit is an array address and the type is an array pointer char(*p)[6], in fact it is just treated as an address and does not really affect the result.

string

    char arr[]="abcdef";
    printf("%d\n", strlen(arr));
    printf("%d\n", strlen(arr+0));
    printf("%d\n", strlen(&arr));
    printf("%d\n", strlen(&arr+1));
    printf("%d\n", strlen(&arr[0]+1));

Insert image description here

When we see this, we actually think of this, isn’t it char arr[] = { 'a','b','c','d','e','f' ,'\0'};? That's right! The calculation method here sizeof()is still the same as above, but strlen()it still needs to be said.
1. strlen(arr);
2. strlen(arr+0);
Since there is '\0'the end, the result must be the length of the string, which is6
3. strlen(&arr);//This will return '\0'the previous number of characters, which is6
4. strlen(&arr+1);//But &arr+1the array is skipped here arr, so arandom value
5. strlen(&arr[0]+1);//This points to 'b'the address of , so return5

Compute constant string

    char *p = "abcdef";
    printf("%d\n", sizeof(p));
    printf("%d\n", sizeof(p+1));
    printf("%d\n", sizeof(*p));
    printf("%d\n", sizeof(p[0]));
    printf("%d\n", sizeof(&p));

Insert image description here
Is it put "abcdef"inside phere? In fact, here pis just 'a'the address
1. sizeof(p);//Here is pthe address of the pointer variable calculated.4 / 8Byte
2. sizeof(p+1);// p+1What is obtained is 'b'the address of the character,4 / 8Byte
3. sizeof(*p);// *pIn fact, it is the first character of the string 'a'.1Byte
4. sizeof(p[0]);//In fact, this is p[0]equivalent to *(p+0)the first element,1Byte
5. sizeof(&p);// The address of &pthe pointer is ptaken out here.4 / 8byte

    char *p = "abcdef";
    printf("%d\n", strlen(p));
    printf("%d\n", strlen(&p));
    printf("%d\n", strlen(&p+1));

Insert image description here

1. strlen(p); //As analyzed above, the address pstored is 'a'the address, and the end strlen()is found '\0', so it is62. 3. // It is the address of the pointer. Here I drew a picture to illustrate: So
what is found here is actually a random value!strlen(&p);
strlen(&p+1);&pp
Insert image description here

Calculate two-dimensional array

    int a[3][4] = {
    
    0};
    printf("%d\n",sizeof(a));
    printf("%d\n",sizeof(a[0][0]));
    printf("%d\n",sizeof(a[0]));
    printf("%d\n",sizeof(a[0]+1));
    printf("%d\n",sizeof(*(a[0]+1)));
    printf("%d\n",sizeof(a+1));
    printf("%d\n",sizeof(*(a+1)));
    printf("%d\n",sizeof(&a[0]+1));
    printf("%d\n",sizeof(*(&a[0]+1)));
    printf("%d\n",sizeof(*a));
    printf("%d\n",sizeof(a[3]));

Let's take a look at the results output by the compiler! ! ! Let’s explain why.
Insert image description here

1. sizeof(a);//The array name here arepresents the entire two-dimensional array, the size is 12 4,36Byte
2. sizeof(a[0][0]);//This is the first element of the array,4Byte
3. sizeof(a[0]);// a[0]It is the first row of the two-dimensional array as the array name of the one-dimensional array, so sizeof()what is sought is the size of the first row of the array, the size is 4
4,16Byte
4. sizeof(a[0]+1);//This a[0]represents the array name of the first row, which represents the address of the first element. In fact, it is the address of the first element of the first row, so it a[0]+1represents the address of the second element of the first row.4 / 8Byte
5. sizeof(*(a[0]+1));//The above *a[0]+1is the second element of the first row,4Byte
6. sizeof(a+1);//Let’s think about whose address this is? ais the array name of the two-dimensional array and is the address of the first element. The first element of the two-dimensional array is its first row, so it is the aaddress of the first row. Then, after adding one, it is the address of the second row.4 / 8Bytes
Insert image description here
Insert image description here
From this we can also see that the difference between the two addresses is 16, which is exactly the size of 4 integers, so this also verifies the above. After adding one, a line is skipped.

7. sizeof(*(a+1));//As in 6., after dereferencing, it represents the second line.16Byte
8. sizeof(&a[0]+1);// &a[0]Take out the first row address, add one to the second row address,4 / 8Byte
9. sizeof(*(&a[0]+1));//Dereference to get the second row of the array,16Byte
10. sizeof(*a);// aIt is the address of the first element, the address of the first line, and it is the first line after dereference.16Byte
11. sizeof(a[3]);// a[3]It is the fourth row of the array. Although there is no fourth row, according to the array type, the calculated array size is four integers in one row, so it is still16

Guess you like

Origin blog.csdn.net/2301_77404033/article/details/132185734