sizeof to find bytes and the difference from strlen

Source: Internet
Author: User

Example one:

/** Calculated according to the following conditions: * 1, the size of the structure is equal to the maximum member size of the structure of the integer times * 2, the first address of members of the structure body relative to the first address of the structure of the offset is an integer multiple of its type size, for example, double members relative to the address of the structure of the first address * The offset should be a multiple of 8. */#include<iostream>#include<cstdlib>using namespacestd;classaa{intA;  Shortb; intC; CharD;};classbb{DoubleA;  Shortb; intC; CharD;};struct{     ShortA1;  ShortA2;  ShortA3;} A;struct{    LongA1;  ShortA2;} B;intMain () {cout<<sizeof(AA) <<" "<<sizeof(BB) <<Endl; Char*ss1="0123456789"; Charss2[]="0123456790"; Charss3[ -]="0123456789"; intss4[ -]; Charq1[]="ABC"; Charq2[]="a\n"; Char*q3="a\n"; Char*str1= (Char*) malloc ( -); void*str2= (void*) malloc ( -); cout<<sizeof(SS1) <<endl;//SS1 is a character pointer, the size of the pointer is a fixed value, that is, 4cout<<sizeof(SS2) <<endl;//SS2 is an array of characters, which is initially undefined and populated by the specific casecout<<sizeof(SS3) <<endl;//SS3 is also an array of characters, which begins to pre-allocate 100, so it's 100 bits in size.cout<<sizeof(SS4) <<endl;//SS4 is also an array of integers, which begins to pre-allocate 100, but the space occupied by each integer variable is 4, so its size iscout<<sizeof(Q1) <<endl;//Q1 and SS2 are similar, so it's 4.cout<<sizeof(Q2) <<endl;//Q2 There is a ' \ n ', counted as one, so its space is 3cout<<sizeof(Q3) <<endl;//Q3 is a character pointer, the size of the pointer is a fixed value, is 4, so sizeof (Q3) is 4cout<<sizeof(A) <<endl;//The length of the structure must be an integral multiple of the longest data element.cout<<sizeof(B) <<Endl; cout<<sizeof(STR1) <<endl;//the length of the pointer is fixed at 4.cout<<sizeof(STR2) <<Endl;}

Where AA, a accounted for 4 bytes, b should have accounted for 2 bytes, but because C accounted for 4 bytes, in order to meet the conditions 2,b more than 2 bytes, in order to meet the conditions 1,d occupy 4 bytes, altogether 16 bytes.
BB, A is 8 bytes, B is 2 bytes, but because C accounts for 4 bytes, in order to satisfy the condition 2,b takes up 2 bytes,
That is, ABC consumes 8+4+4=16 bytes,
In order to satisfy the condition 1,d will occupy 8 bytes, altogether 24 bytes.

Example two:

#include <iostream>#include<New>using namespacestd;classa{};classa2{Chard,e;};classb{};structc{Charb,c;};structd{intx, y;};intMain () {cout<<sizeof(A) <<endl;//for a class, even if it is an empty class, the compiler will still give it a space, so class A, even if there is nothing, its space size is also 1cout<<sizeof(A2) <<endl;//The class A2 size is the sum of two characters D and E in the class, so its space size remains 2.A *p1=NewA ();    A P2; A*P3; //As for P1 P2 p3,p1 and P3 are pointers, so their size is certain, so 4,p2 is the object of Class A, so its size is equal to Class A and is also 1cout<<sizeof(p1) <<Endl; cout<<sizeof(p2) <<Endl; cout<<sizeof(p3) <<Endl; cout<<sizeof(B) <<Endl; cout<<sizeof(C) <<Endl; cout<<sizeof(D) <<Endl;}

Example three:

Summary: Empty classes also take up memory space, and the size is 1, because C + + requires each instance to have a unique address in memory.

1) member variables inside the class:

Common variables: Use memory, but be aware of memory alignment (this is similar to struct type)

Static variable modified statically: does not consume memory because the compiler places it in the global variable area

2) member functions inside the class

Non-virtual functions (constructors, static functions, member functions, and so on): Do not consume memory

virtual function: To occupy 4 bytes (32 operating system), to specify the entry address of the virtual function table. is not related to the number of virtual functions. A virtual function pointer is shared between the parent and child classes.

The only data that makes up the object itself is that no member function is subordinate to any object, and the non-static member function is bound to the object, and the binding mediation is the this pointer. member functions are shared for objects of this class, not only in simplifying language implementations, saving storage, but also by consistent behavior for homogeneous objects. Homogeneous objects behave as though they are consistent, but manipulate different data members.

#include <iostream>using namespacestd;classa1{ Public:    intA; Static intb//static in the global variable area, not part of the objectA1 (); ~A1 ();};classa2{ Public:    intA; CharC;    A2 (); ~A2 ();};classa3{ Public:    floatA; CharC;    A3 (); ~A3 ();};classa4{ Public:    floatA; intb; CharC;    A4 (); ~A4 ();};classa5{ Public:    DoubleD; floatA; intb; CharC;    A5 (); ~A5 ();};intMain () {cout<<sizeof(A1) <<Endl; cout<<sizeof(A2) <<Endl; cout<<sizeof(A3) <<Endl; cout<<sizeof(A4) <<Endl; cout<<sizeof(A5) <<Endl;}

The size of sizeof when there are parent and child classes and virtual inheritance

Summarize:

    • The size of the class is the sum of the type size of the non-static data members of the class, and static member data is not considered
    • The size of the empty class is 1, the empty class space for single inheritance and multiple inheritance is 1, and the virtual inheritance is 4
    • constructors, destructors, and normal member functions are not related to sizeof.
    • Class with virtual function, because a virtual function table is maintained so that it occupies a pointer space 4
    • Subclass if is a virtual function of the re-implemented parent class, not counted to sizeof size
    • Virtual inheritance involves virtual tables (virtual pointers) with a size of 4

For example:

#include <iostream>#include<memory>#include<assert.h>using namespacestd;classa{};classa2{};classB: Publica{};classC: Public Virtualb{};classD: PublicA Publica2{};intMain () {cout<<"sizeof (A):"<<sizeof(A) <<Endl; cout<<"sizeof (B):"<<sizeof(B) <<Endl; cout<<"sizeof (C):"<<sizeof(C) <<Endl; cout<<"sizeof (D):"<<sizeof(D) <<Endl;}

Output:

The difference between sizeof and strlen:

Example one:

Char *ss= "0123456789";

sizeof (SS) results for 4,SS are character arrays that point to string constants.

sizeof (*SS) result is 1,*ss is the first character

Example two:

Char ss[]= "0123456789";

The sizeof (SS) result for 11,ss is an array, which computes to the ' \ s ' position and therefore is (10+1).

sizeof (*SS) result is 1,*ss is the first character

Example three:

Char ss[100]= "0123456789";

The sizeof (SS) result is that 100,SS represents the size that is pre-allocated in memory, 100*1.

The strlen (ss) result is 10, and its internal implementation is to use a loop to calculate the length of the string until ' \ ' and does not contain '/'.

Example four:

int ss[100]= "0123456789";

The sizeof (SS) result is that 400,SS represents the size in memory, 100*4.

STRLEN (ss) error, the strlen parameter can only be char* and must be terminated with '. '

Example five:

Class X

{

int i;

Int J;

Char K;

};

x x;

Cout<<sizeof (X) <<endl; The result is 12, the memory is padded

Cout<<sizeof (x) <<endl; The result is 12, the reason is ditto

Through the above-mentioned sizeof and Strlen's deep understanding, the difference is as follows:

1) The structure type of the sizeof operator is size_t, and its typedef in the header file is the unsigned int type. This type guarantees that the byte size of the largest object being built can be accommodated.

2) sizeof is an operator, strlen is a function

3) sizeof can use the type parameter, strlen can only use char* to do the parameters, and must be "" "to end. sizeof can also use functions to make arguments, such as:

Short f ();

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

The result of the output is sizeof (short), which is 2

4) The parameters of the sizeof array do not degenerate, passed to the strlen is degraded to a pointer.

5) Most compilers have calculated sizeof at compile time, which is the length of the type or variable. This is why sizeof (x) can be used to define the number of dimensions of an array:

Char str[20]= "0123456789";

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

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

6) The results of the strlen are calculated at run time, and are used to calculate the length of the string, not the size of the type in memory.

7) After sizeof if the type must be parentheses, if the variable name can be no parentheses. This is because sizeof is an operator and not a function.

8) When a struct type or variable is used, sizeof returns the actual size. When a static array space is used, sizeof returns the dimensions of all arrays. The sizeof operator cannot return the dimensions of an array that is dynamically allocated or an outer array.

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*). Passing an array in C + + is always 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 inside a function, you need to do this: Copy the array with memcpy after entering the function, and the length is passed in by another parameter. The code is as follows:

Char *p1,int  len) {    char *buf=newchar[len+1  ];    memcpy (Buf,p1,len);}

10) to calculate the size of the structure variable, the data alignment problem must be discussed. In order to make the CPU access the fastest, C + + in the processing of data often the size of the members of the structure variable in terms of 4 or 8, which is called data alignment.

One) The sizeof operator cannot be used for a function type, an incomplete type, or a bit field. An incomplete type is a data type that has unknown storage size data, such as an array type that does not have a storage size, a struct or union type of unknown content, a void type, and so on.

sizeof for pointers, the size of the pointer is calculated, if the operand is an array parameter in a function or a parameter of the function type, sizeof gives the size of its pointer.

For example:

Char var[10];

int Test (char var[])

{

return sizeof (VAR);

};

sizeof to find bytes and the difference from strlen

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.