sizeof and strlen

sizeof is calculated will be the result of a number of bytes type of space occupied at compile time, so to make an array name parameter calculation is the size of the entire array. The strlen is only the beginning of the calculation results at run time, which is the result of the calculation is no longer the size of the type of memory occupied by the array name degenerates to a pointer.

strlen (): function strlen start looking for the number of characters from the specified address, to appear between the first 0

the sizeof () operator

sizeofhttps://blog.csdn.net/weixin_41042404/article/details/86719441

There are many in this article is from https://blog.csdn.net/weixin_41042404/article/details/86719441 handling, thanks to the authors of the paper, we reprint this article do not forget to attach a link.

The following is a C program, comments are introduced corresponding knowledge

#include<stdio.h>

#include<string>

#include <stdlib.h>

 

typedef long TLONG;

int f1() { return 0; };

double f2() { return 0.0; }

void f3() {}

union u1 // define joint

{

    double a;

    int b;

};

union u2

{

    char a[13];

    int b;

};

 

union u3

{

    char a[13];

    char b;

};

 

struct s1

{

    char a;

    double b;

    int c;

    char d;

};

 

struct s2 // definition of the structure

{

    char a;

    char b;

    int c;

    double d;

};

 

int main ()

{

    char const *str1 = "123456";

    char const str2[] = "123456";

    char const str3[] = {1, 2, 3, 4, 5, 6};

    double* (*a)[3][6];

 

    printf ( "sizeof (* str1) =% d, sizeof (str1) =% d, strlen (str1) =% d \ n", sizeof (* str1), sizeof (str1), strlen (str1)); // 1 4 6, * str1 first is the first character, str1 is a pointer, sizeof (pointer) = 4 strlen number of characters before statistical str 0

    printf ( "sizeof (str2) =% d, strlen (str2) =% d \ n", sizeof (str2), strlen (str2)); // 7 6, sizeof (str2 the \ 0 was added calculation, strlen calculated only \ 0 before

    printf ( "sizeof (str3) =% d, strlen (str3) =% d \ n", sizeof (str3), strlen (str3)); // 6 undefined, Str3 is an array, not a string, strlen undetectable \ 0 will continue

    printf ( "sizeof (int) =% d, sizeof (1 == 2) =% d \ n", sizeof (int), sizeof (1 == 2)); // 1 == 2 type corresponds bool

    printf ( "sizeof (unsigned int) == sizeof (int) is% d \ n", sizeof (unsigned int) == sizeof (int)); signifies only the highest bit // 1, unsigned effects, data length will not be changed.

    printf ( "type definitions and Comparative prototypes = 1% d \ n", sizeof (long) == sizeof (TLONG)); // 1 custom type sizeof type value equal to its original shape.

    printf("sizeof 2=%d,sizeof(2)=%d\n", sizeof 2, sizeof(2));//正确

// printf ( "sizeof int =% d, sizeof (int) =% d \ n", sizeof int, sizeof (int)); // Conclusion: The error can not add sizeof () regardless of the value of sizeof to whom, The best are plus ().

    printf("sizeof(f1())=%d,sizeo(f2())=%d\n", sizeof(f1()), sizeof(f2()));//正确

    // printf ( "sizeof (f3 ()) =% d \ n", sizeof (f3 ())); // Error Conclusion: sizeof a function, the function type of return value will be substituted in the translation stage,

    printf("sizeof(int*)=%d,sizeo(char*)=%d,sizeo(double*)=%d\n",

        sizeof (int *), sizeof (char *), sizeof (double *)); // 4 4 4 Conclusion: As long as the pointer size is four. (64-bit machine is not necessarily to become 8).

    printf("sizeof(a)=%d,sizeof(*a)=%d,sizeof(**a)=%d,sizeof(***a)=%d,sizeof(****a)=%d\n",

        sizeof(a), sizeof(*a), sizeof(**a), sizeof(***a), sizeof(****a));

    / * Since the implementation of a double * [3] [6] of pointers,

    * A to represent a double * [3] [6] The multi-dimensional array type,

    因此sizeof(*a)=3*6*sizeof(double*)=72。

    The same, ** a represents a double * [6] of the array type,

    所以sizeof(**a)=6*sizeof(double*)=24。

    *** a, says one of the elements, which is a double *,

    Therefore, sizeof (*** a) = 4. As **** a, it is a double, and

    所以sizeof(****a)=sizeof(double)=8。*/

    printf("sizeof(u1)=%d,sizeof(u2)=%d,sizeof(u3)=%d\n",

        sizeof(u1), sizeof(u2), sizeof(u3));

    / * Union's size depends on all its members, the size of the space occupied by one of the largest members.

    So for u, the size is the largest member of a type of double, so sizeof (u) = sizeof (double) = 8.

    But for u2 and u3, the largest space is char [13] An array of type, size u3 Why are 13, 16 and u2 is it?

    The key is u2 a member of the int b. Due to the presence of type int member, so that the alignment u2 becomes 4, i.e.,

    u2 in the size of the sector to be 4, so that the space occupied becomes 16 (closest to the boundary 13).

*/

    printf("sizeof(s1)=%d,sizeof(s2)=%d\n",

        sizeof(s1), sizeof(s2));

    / * For s1, first put into a sector of 8, is assumed to be 0, then the next free address is 1,

    But in a double type element is d, 8 to be put on the boundary of the address 1 from the closest 8,

    D 8 is placed so that, at this time into a next free address 16, the next element of the sector c is 4,

    16 meet, so 16 c placed at this time into a next free address 20, the next element of the sector need to d 1,

    Also fall exactly on the boundary, the resulting 20 d in the structural body 21 ends at the address.

    Since the size s1 needs to be a multiple of 8, so that a space is reserved 21-23, into a size of 24 s1.

    For s2, first put into a sector of 8, is assumed to be 0, then the next free address is 1,

    Bounds on the element is a 1, so 1 b placed in the next free address becomes 2;

    The next element of the sector c is 4, it is taken from the address of the nearest 2 c 4 placed, the next free address becomes 8,

    The next element of the boundary of d is 8, so d placed in 8, all elements placed finished, at the end of the structure 15,

    The total space occupied by 16, is exactly a multiple of eight. * /

   

    system ( "pause"); // role is to make the program pause.

 

    return 0;

}

 

 

 

operation result:

sizeof(*str1)=1,sizeof(str1)=4,strlen(str1)=6

sizeof(str2)=7,strlen(str2)=6

sizeof(str3)=6,strlen(str3)=22

sizeof(int)=4,sizeof(1==2)=1

sizeof(unsigned int) == sizeof(int)值为1

Comparison of Prototype and custom type = 11

sizeof 2=4,sizeof(2)=4

sizeof(f1())=4,sizeo(f2())=8

sizeof(int*)=4,sizeo(char*)=4,sizeo(double*)=4

sizeof(a)=4,sizeof(*a)=72,sizeof(**a)=24,sizeof(***a)=4,sizeof(****a)=8

sizeof(u1)=8,sizeof(u2)=16,sizeof(u3)=13

sizeof(s1)=24,sizeof(s2)=16

strlen (str3) = 22 The reason why is uncertain because strlen needs to see '\ 0' will stop, if

char const str3[] = {1, 2, 3, 4, 5, 6,0}; strlen(str3)=6

If not const, it will have problems running

 

Guess you like

Origin www.cnblogs.com/bihua/p/11696018.html