Detailed explanation of sizeof and strlen (64-bit platform)
Comparison of sizeof and strlen
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。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! ! !
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:
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+0
is an expression that does not satisfy the form of sizeof (array name), so here a
is still the address of the first element, a+0
and the address of the first element, and the address size is4 / 8Byte
3. sizeof(*a);
//Here a
is the address of the first element, so *a
it is the size of the type ( int
) of the first element, which is4
4. sizeof(a+1);
//Here a
is the address of the first element, which a+1
is 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);
// &a
What is taken out is the address of the array, all addresses are4 / 8
7. sizeof(*&a);
// It is the address of &a
the array . After dereference, it is the array . The size of the array is calculated here.a
*
a
16Byte
8... sizeof(&a+1);
//Based on the above, it is not difficult to imagine that this &a+1
is equivalent to &a
skipping 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:
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 *arr
is 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, *arr
but a character a
, ASCII
the 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.
This 0x0000000000000061
is actually 97, which confirms our statement.
5. strlen(&arr);
6. strlen(&arr+1);
7. strlen(&arr[0]+1);
&arr
, &arr+1
, &arr[0]+1
are 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 &arr
it 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));
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+1
the 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));
Is it put "abcdef"
inside p
here? In fact, here p
is just 'a'
the address
1. sizeof(p);
//Here is p
the address of the pointer variable calculated.4 / 8Byte
2. sizeof(p+1);
// p+1
What is obtained is 'b'
the address of the character,4 / 8Byte
3. sizeof(*p);
// *p
In 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 &p
the pointer is p
taken out here.4 / 8byte
char *p = "abcdef";
printf("%d\n", strlen(p));
printf("%d\n", strlen(&p));
printf("%d\n", strlen(&p+1));
1. strlen(p);
//As analyzed above, the address p
stored 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);
&p
p
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.
1. sizeof(a);
//The array name here a
represents 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]+1
represents the address of the second element of the first row.4 / 8Byte
5. sizeof(*(a[0]+1));
//The above *a[0]+1
is the second element of the first row,4Byte
6. sizeof(a+1);
//Let’s think about whose address this is? a
is 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 a
address of the first row. Then, after adding one, it is the address of the second row.4 / 8Bytes
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);
// a
It 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