Sizeof is an operator that returns the memory bytes occupied by an object or data type.
The strlen (char *) function is used to calculate the actual length of the string. The method is from the beginning to the First '\ 0'. If you only define that the initial value is not assigned to it, the result is not correct. It will be searched from the first address of AA until '\ 0' is stopped. Strlen returns the Valid String Length, excluding the terminator '\ 0 '. The strcpy function is copied together with '\ 0.
Basic Type sizeof instance
# Include "stdafx. H "# include <stdlib. h ># include <iostream> using namespace STD; void teststrlen () {char AA [10]; char BB [10] = {'\ 0 '}; char CC [10] = "Jun"; cout <strlen (AA) <Endl; // The result is an indefinite cout <strlen (bb) <Endl; // The result is 0 cout <strlen (CC) <Endl; // The result is 3 cout <sizeof (CC) <Endl; // 10 while sizeof () the function returns the amount of memory occupied after the variable declaration, not the actual length.} Void * fun1 () {printf ("Void \ n"); return NULL;} Char fun2 () {char c = 'C '; printf ("char c = 'C'"); Return C;} int fun3 () {int I = 2; printf ("int I = 2"); return I ;} struct strc0 {}; struct strc00 {int geta () ;}; struct strc000 {int * geta () {return NULL ;}}; struct strc1 {int; char B ;}; struct strc2 {char B; int A ;}; struct strc3 {char B; int A; double C ;}; struct strc4 {double C; char B; int A ;}; struct strc5 {Cha R B; double C; int A ;}; struct strc6 {int A; double C; char B ;}; struct strc7 {int A; double C; char B; char D ;}; class classa0 {}; class classa00 {int geta (); char getb () ;}; class classa000 {int geta () {return 1 ;} char getb () {return 'C' ;}; class classa1 {int A; Double B ;}; class classa2 {char B; double C; int ;}; class classa3 {char B; double C; int A; char D ;}; int _ tmain (INT argc, _ tchar * argv []) {Teststrlen (); // basic data type printf ("% d \ n", sizeof (char); // 1 printf ("% d \ n ", sizeof (short); // 2 printf ("% d \ n", sizeof (INT); // printf ("% d \ n ", sizeof (2); 4 printf ("% d \ n", sizeof (long); // 4 printf ("% d \ n", sizeof (float )); // 4 printf ("% d \ n", sizeof (double); // printf ("% d \ n", sizeof (2.12 )); 8 printf ("% d \ n", sizeof (string); // 32 // function: Check the returned value, which is irrelevant to the internal applied variable, and no function printf ("% d \ n", sizeof (fun1 () is called; // 4 // no function printf ("% d \ N ", sizeof (fun2 (); // 1 printf (" % d \ n ", sizeof (fun3 (); // 4 // pointer: 4 printf ("% d \ n", sizeof (void *); // 4 printf ("% d \ n ", sizeof (int *); // 4 // array: its value is (subscript * Data Type). When the array is a parameter, it becomes the pointer int A1 [4]; char A2 [] = "ABCD"; // char A2 [4] = "ABCD"; // array limit overflow char A3 [6] = "ABCD \ 0 "; // description "\ 0" is a character (Escape Character) Char A4 [20] = "ABC"; printf ("% d \ n", sizeof (A1 )); // 4*4 = 16 printf ("% d \ n", sizeof (A2); // 5, there is also a null ending character printf ("% d \ n", sizeof (A3) at the end of the character )); // 6 printf ("% d \ n", sizeof (A4); // 20, indicating that 20 bytes of memory is allocated to A4, whether or not it is filled with printf ("% d \ n", sizeof (A4 [10]); // 1, A4 [4] is a char type data // struct, it involves the issue of byte alignment, and the details of byte alignment are related to compiler implementation. // The total size of the struct is an integer multiple of the size of the widest basic type of the struct. // The offset (offset) of each struct member relative to the first address of the struct) all are integer multiples of the member size. // sum up by yourself: the size of the struct = the size of the widest basic type member + the number of its before and after measurement. Its member functions do not occupy the memory printf ("% d \ n", sizeof (strc0); // 1, the empty struct occupies 1 printf ("% d \ n", sizeof (strc00); // 1, functions defined in the structure do not occupy the memory printf ("% d \ n", sizeof (strc000); // 1. the functions defined in the structure do not occupy the memory, no matter what the return value is. Printf ("% d \ n", sizeof (strc1); // 8, indicating the occurrence of character alignment in the struct printf ("% d \ n ", sizeof (strc2); // 8 printf ("% d \ n", sizeof (strc3); // instead of 24, 16 printf ("% d \ n ", sizeof (strc4); // 16 printf ("% d \ n", sizeof (strc5) instead of 24; // not 16, instead, it is 24 printf ("% d \ n", sizeof (strc6); // instead of 16, it is 24 printf ("% d \ n", sizeof (strc7 )); // 24 // union Union U {char * A; Double B; float C ;}; cout <"sizeof (uniion u) =" <sizeof (Union U) <Endl; // The 8 struct is sequential in the memory structure, The Community is overlapping, and each member shares a piece of memory. Therefore, the sizeof of the entire community is the maximum sizeof each member. // Class: Same as struct. Class size = the size of the widest basic type member + the number of its before and after measurement. Member functions do not occupy the memory printf ("% d \ n", sizeof (classa0); // 1 printf ("% d \ n", sizeof (classa00 )); // 1, indicating that the declared function does not occupy the memory printf ("% d \ n", sizeof (classa000); // 1, the defined function does not occupy the memory printf ("% d \ n", sizeof (classa1); // 16, description: printf ("% d \ n", sizeof (classa2); // 24 printf ("% d \ n ", sizeof (classa3); // 24 system ("pause"); Return 0 ;}
Conclusion:Class/struct size = the widest member type + the Number of pre and post measurements. Common member functions (including pointer and static functions, excluding virtual functions) do not occupy memory, whether or not they return values.
Class (virtual function, abstract function, static variable, static function, virtual inheritance) sizeof instance
# Include "stdafx. h "# include <iostream> using namespace std; // The Compiler creates a virtual function table for each class with virtual functions (its size is not calculated in the class ), insert A pointer to the virtual function table for this class, that is, the size of each class with a virtual function is at least the size of A pointer 4 class a {public: int; void Function () ;}; class B {public: int a; virtual void Function () ;}; class C: public B {public: char B ;}; class D: public B {public: virtual void Function2 () ;}; class E {public: static void Function () ;}; class staticE {static int I NtVar; static void fun () {}}; void test1 () {cout <"sizeof (A) =" <sizeof (A) <endl; // 4 (contains an int, common functions do not occupy the size) cout <"sizeof (B) =" <sizeof (B) <endl; // 8 (an int, A virtual function table pointer) cout <"sizeof (C) =" <sizeof (C) <endl; // 12 (an int, a virtual function table pointer, a char, coupled with Data Alignment) cout <"sizeof (D) =" <sizeof (D) <endl; // 8 (an int, one virtual function table pointer. Multiple virtual functions are placed in one table. Therefore, only one virtual function table pointer can be used.) cout <"sizeof (E) = "<sizeof (E) <endl; // 1 (static function occupies no size, empty class size is 1) cout <" sizeof (StaticE) = "<sizeof (staticE) <endl; // 1 static data member is not included in the class} class cA {public: virtual void PrintA1 (void) {} virtual void PrintA2 (void) {}}; class cB {public: virtual void PrintB (void) {}}; class cC {public: virtual void PrintC (void) {}}; class cD: public cA, public cB, public cC {}; class cE: public cA, public cB, public cC {public: virtual void PrintE (void) {}}; void test2 () {cout <"sizeof (cD) =" <siz Eof (cD) <endl; // 12 if nothing is implemented in a class and only one or more virtual functions are implemented, the sizeof test will get 4, however, if a class inherits from multiple classes and multiple base classes have virtual functions, it will have multiple virtual function tables, which are also reflected in COM. cout <"sizeof (cE) =" <sizeof (cE) <endl; // 12} class dA {int a; virtual ~ DA () {}}; class dB: virtual public dA {virtual void myfunB () {}}; class dC: virtual public dA {virtual void myfunC (){}}; class dD: public dB, public dC {virtual void myfunD () {}}; void test3 () {cout <"sizeof (dA) =" <sizeof (dA) <endl; // 8 cout <"sizeof (dB) =" <sizeof (dB) <endl; // 12 cout <"sizeof (dC) = "<sizeof (dC) <endl; // 12 cout <" sizeof (dD) = "<sizeof (dD) <endl; // 16 // explanation: int + virtual table pointer in. In B and C, because it is A virtual inheritance, the size is A + pointer to the virtual base class. Although B and C are added with their own virtual functions, however, the virtual table pointer is shared with the base class, so it does not have its own virtual table pointer. D Because B and C are virtual inheritance, D only contains A copy of, the size of D is equal to the pointer pointing to the virtual base class in A + B + the pointer pointing to the virtual base class in C. // If B and C are not virtual inheritance but normal inheritance, the size of A, B, and C is 8 (no pointer pointing to the virtual base class ), because D is not A virtual inheritance, it contains two A copies with A size of 16. note that although the size of D is the same as that of virtual inheritance, the memory layout is different .} Class T1 {}; class T2: public T1 {}; class T3: public T1 {int a ;}; class M1 {virtual void GetM () = 0 ;}; class M2 {virtual void GetM2 () = 0; virtual void Get () ;}; class M3: public M1, public M2 {int a ;}; void test4 () {cout <"sizeof (T2) =" <sizeof (T2) <endl; // 1 inherits an empty parent class. In this case, sizeof (parent class) = 0 cout <"sizeof (T3) =" <sizeof (T3) <endl; // 4 same as cout <"sizeof (M1) = "<sizeof (M1) <endl; // 4 cout <" sizeof (M2) = "<sizeof (M2) <Endl; // 4 cout <"sizeof (M3) =" <sizeof (M3) <endl; // 12 abstract functions are the same as virtual functions} void main () {test1 (); test2 (); test3 (); test4 ();}/* virtual function table (http://www.uml.org.cn/c%2B%2B/200811143.asp) the role of virtual functions in C ++ is to implement the polymorphism mechanism. With regard to polymorphism, in short, the pointer of the parent type points to the instance of its subclass, and then calls the member function of the actual subclass through the pointer of the parent class. This technology enables the pointer of the parent class to have multiple forms. This is a generic technology. The so-called generic technology, to put it bluntly, is to try to use the same code to implement a variable algorithm. For example, the template technology, RTTI technology, and virtual function technology either try to achieve resolution at compilation or at runtime. Anyone familiar with C ++ should know that Virtual functions are implemented through a Virtual Table. V-Table for short. In this table, the primary table is the address table for a class virtual function. This table solves the inheritance and overwrite issues and ensures that it can reflect the actual functions. In this way, the table is allocated to the memory of the instance in instances of classes with virtual functions. Therefore, when we use the parent class pointer to operate a subclass, this virtual function table is very important. Like a map, it specifies the actually called function. Here we will focus on this virtual function table. The C ++ compiler should ensure that the pointer to the virtual function table exists in the front of the object instance (this is to ensure the highest performance of the virtual function table-if there are multi-layer inheritance or ). This means that we can get this virtual function table through the address of the object instance, then we can traverse the function pointer and call the corresponding function. During debugging, you can see in the local variable window that the virtual function table */
Note: struct is similar to class.
Seriously, I found that memory alignment (byte alignment and struct alignment)