The difference and connection between c++sizeof and strlen

Source: Internet
Author: User

First, sizeof
sizeof (...)is an operator that typedef in the header fileto unsigned int, the values are calculated at compile time, and 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, sizeofcannot be used to return the size of a dynamically allocated memory space. In fact, using sizeofTo return the space occupied by the type and statically allocated objects, structures, or arrays, the return value has nothing to do with what the object, structure, or array stores.
Specifically, when the parameters are as follows, sizeofThe value returned represents the following meaning:
Arrays--The size of the array space allocated at compile time;
Pointers--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 size of the space occupied by the type;
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。
*****************

Third, 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
Reviews: sizeofreturn definition arrArray, the compiler allocates the size of the array space, and does not care how much data is stored in it. StrlenOnly cares about the stored data content, not the size and type of space.

Ggp, char * parr = new CHAR[10];
int len_one = strlen (Parr);
int len_two = sizeof (Parr);
int len_three = sizeof (*parr);
cout << len_one << "and" << len_two << "and" << len_three << Endl;
Output Result: 4 and 1
Reviews: First output result 23Each run may actually be different, depending on the ParrWhat's in store (from Parr[0)Start to know the first null encounteredend); The second result is actually intended to be a computational parr.The size of the dynamic memory space pointed to, but it backfired, sizeofThink Parris a character pointer, so returns the space occupied by the pointer (the pointer is stored with a long integer, so 4);The third result, due to the *parrIt represents the Parr.The address space that is meant to hold the character, so the length is 1。
************

Iv. References:
Sizeofand strlenThe Difference and contact (Go

1.sizeofThe result type of the operator is size_t, it is a typedef in the header fileto unsigned intType.
This type guarantees that the byte size of the largest object being built can be accommodated.

2.sizeofIt's an operator, strlen.is a function.

3.sizeofYou can use the type parameter, strlenOnly with char*.parameters, and must be "End of.
sizeofYou can also use functions to make arguments, such as:
Short f ();
printf ("%d\n", sizeof (f ()));
The result of the output is sizeof (short), that is, 2。

4.Array to do sizeofThe parameters are not degraded and passed to the strlenIt is reduced to a pointer.

5.Most compiled programsAt compile time, the sizeofIt's been calculated.is the type or the length of the variable this is sizeof (x)Reasons that can be used to define the number of array dimensions
Char str[20]= "0123456789";
int A=strlen (str); a=10;
int b=sizeof (str); //and b=20;

6.strlenResults are calculated at run time, and are used to calculate the length of the string, not the size of the memory.

7.sizeofIf the type must be bracketed, the variable name can be non-parenthesized. This is because sizeofAn operator is not a function.

8.When applied to a struct type or variable, sizeofReturns the actual size,
When applied to a static space array, sizeofReturns the dimensions of all arrays.
sizeofThe operator cannot return the dimensions of an array or an outer array that has been dynamically dispatched

9.The array is passed as a parameter to the function simultaneous is a pointer instead of an array, passing the first address of the array,
Such as:
Fun (char [8])
Fun (char [])
is equivalent to Fun (char *)
In C + +The parameter passing array is always passed a pointer to the first element of the array, and the compiler does not know the size of the array
If you want to know the size of an array within a function,Need to do this:
Use memcpy after entering functionCopy it, 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 use sizeof a lot.and strlen, the length of the string array is usually calculated
Read the above detailed explanation, found that the use of the two are still different, from this example can be seen very clearly:

Char str[20]= "0123456789";
int A=strlen (str); a=10; >>>> strlenComputes the length of the string to the Terminator 0x00Ends for the string.
int b=sizeof (str); //and b=20; >>>> sizeofThe allocated array is computed STR[20]
above is the result of a static array processing, and if it is a pointer, the result is different

char* ss = "0123456789";
sizeof (ss) result 4 === "SS Get a pointer to the space occupied by, Long Integer, so is 4
sizeof (*SS) result 1 === "*ss is the first character
type, accounted for 1 If you want to get the length of this string, be sure to use strlen

C++sizeof and strlen differences and connections

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.