sizeof 与strlen

A, sizeof

    the sizeof (...) is the operator, as typedef unsigned int in the header file, i.e., the value at compile time calculate the parameter can be an array, a pointer, type, object, functions, and so on.

    Its functions are: to obtain assurance can accommodate maximum target established by the byte size.

    Since computed at compile time, and therefore the size can not be used to return sizeof dynamically allocated memory space. In fact, to the return type and statically allocated with the sizeof the object, structure, or an array of space occupied, with the return value of the object, structure, content stored in the array does not matter.

    Specifically, when the parameters are as follows, showing the meaning of the value returned by sizeof follows:

    Array - an array of compile-time allocation of space;

    Pointer - storage space used by the pointer (the address pointer memory length is long integer, it should be 4);

    Type - the type of occupied space;

    Object - The actual size of the object space;

    Function - the function's return type of occupied space. The return type of the function can not be void.

**************

Two, strlen

    strlen (...) is a function, in order to be evaluated at run time. Argument must be a character pointer (char *). When the array name passed as a parameter, in fact it degenerate into an array of pointers.

    Its function is: Returns the length of the string. The string may be their own definition, it may be random memory, the function actually performs the function from the first address representative of traversing the string until it encounters the terminator NULL. The length does not include the size of the return NULL.

*****************

Third, for example:

    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

    Comments: sizeof returns an array arr is defined, the compiler for its array of distribution space, do not care how much data is stored inside. strlen only interested in data stored content, do not care about the size and type of space.

    eg2、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: 23 and 4 and 1

    Comments: The first output of each run 23 actually may be different, depending on what parr kept inside (from parr [0] start end of the first known encounter NULL); The second result is actually meant to want to calculate the size of the dynamic memory space pointed parr, but it did not, sizeof is considered parr character pointer, so that the returned pointer to the space occupied (pointer storing the long integer, so 4); a third a result, since the * represents parr parr referred character storage address space, the length of one.

************

Fourth, Reference:

Sizeof and Strlen differences and relations (rpm)

1.sizeof operator result type is size_t, it is in the header file typedef unsigned int types.

This type of guarantee can accommodate maximum established target byte size.

2.sizeof is the operator, strlen is a function. Statement sizeof (int) sizeof described can indeed not a function, a function of receiving as parameter (a variable) without a C / C ++ function of receiving a data type (e.g., int) is the "parameter" the world.

3.sizeof can do with the type of the parameter, strlen can only do char * parameters, and must be based on '' ending '\ 0' is.

sizeof can do with the function parameters, such as:

short f();

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

Result output is sizeof (short), i.e. 2.

4. sizeof array parameters do not degraded, is transmitted to the pointer strlen degenerates.

5. Most put sizeof calculated compiler at compile time is a length or a variable of the type which is sizeof (X) may be used to define the reason for the array dimension

char str[20]="0123456789";

int a=strlen(str); //a=10;

int b = sizeof (str); // and b = 20;

6.strlen results can be computed at run time, when used to calculate the length of the string, not the type of total memory size.

After 7.sizeof If type must be added in parentheses, if a variable name may not add parentheses. This is because the operator is not sizeof functions.

8. When applied at the time of a variable type or structure, sizeof return the actual size,

When applied to a static space arrays, sizeof return the size of the entire array.

sizeof operator returns the size of the array can be dynamically assigned or outside the array

9. Array transfer time is passed to the function as an argument a pointer instead of an array, the address passed is the first array,

Such as:

fun(char [8])

fun(char [])

They are equivalent to the fun (char *)

Array parameters are passed in C ++ will always pass a pointer pointing 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 within the function, you need to do:

After entering the copy function is used memcpy out length parameter by another pass in

fun(unsiged char *p1, int len)

{

unsigned char* buf = new unsigned char[len+1]

memcpy(buf, p1, len);

}

We can often sizeof and strlen when used, typically calculate the length of an array of strings

Read a detailed explanation of the above, we found that the use of the two is still a difference, from this example you can see very clearly:

char str[20]="0123456789";

int a = strlen (str); // a = 10; >>>> strlen calculated length of the string to the end of the end character string is 0x00.

int b = sizeof (str); // and b = 20; >>>> sizeof calculated array is allocated str [20] The size of the memory space occupied, without changing the content stored inside.

The above is the result of a static array processing, if it is a pointer, the result is not the same

char* ss = "0123456789";

sizeof (ss) result 4 === "ss pointer is a pointer to a character string constants, sizeof obtained is a pointer to the space occupied, it should be

Long integer, it is 4

sizeof (* ss) results 1 === "* ss is the first character is actually won the first '0' memory space occupied by the string is char class

Type, accounting for a

strlen (ss) = 10 >>>> To get the length of the string, you must use strlen

 

Guess you like

Origin blog.csdn.net/qq_29250265/article/details/94546606