C ++ sizeof object size sorting, sizeof object

Source: Internet
Author: User

C ++ sizeof object size sorting, sizeof object

1. sizeof is an operator, not a function.

2. When the sizeof object is an expression, the size of the sizeof object is the type size of the expression returned value, but does not calculate the value of the expression. For example:

1 char c = 1;2 int i = 2;3 cout << sizeof(c + i) << endl;4 cout << sizeof(c = c + i) << endl;

The former c + I is implicitly converted to the int type (the type is increased), SO 4 (32-bit System) is returned, while the latter is also converted to the int type during operation, but when it is assigned to c, it is converted to char. Therefore, 1 is returned.

3. If the object is a function, the return value type of the function is returned, for example:

 1 long long foo() 2 { 3     cout << "foo() has been called" << endl; 4     return 0; 5 } 6  7 int main(int argc, char **argv) 8 { 9     cout << sizeof(foo()) << endl;10     return 0;11 }

Output 8 after execution, but does not output foo () has been called. This indicates that the function is not actually executed, but only determines the type of the next return.

4. When the sizeof object is an array, returnTotal array sizeWhen the object is a pointerPointer sizeInstead of the memory size. Because the pointer itself is an unsigned integer, The Size returned by int * p and sizeof (p) is sizeof (void *), and the 32-bit system returns 4, that is, 32-bit. Note:When the array name is passed into the function as a real parameter, it is automatically converted to the pointer type., As follows:

 1 void foo(int a[]) 2 { 3     cout << sizeof(a) << endl; /* 4 */ 4 } 5 int main(int argc, char **argv) 6 { 7     int a[] = {1, 2, 3, 4}; 8     int *p = a; 9     cout << sizeof(a) << endl; /* 16 */10     cout << sizeof(p) << endl; /* 4 */11     foo(a);12     return 0;13 }

5.Sizeof cannot obtain the size of dynamically allocated memory, that is, it cannot be used to obtain the size of dynamically allocated memory by using malloc.

6. Note that there is a \ 0 terminator at the end of the c_style string, which also occupies a char space. Therefore, sizeof ("1") returns 2. Strlen returns the number of characters, excluding the \ 0 Terminator.

7. In theory, the space occupied by a struct is the total size of all members. However, given the alignment, there will be padding bytes.

struct node1{    int a;    int b;    char c;    char d;};struct node2{    int a;    char b;    int c;    char d;};

The size of node1 is 12 bytes, and that of node2 is 16 bytes.

The dynamic array after the struct, that is, an array with no size specified. The size of the dynamic array is not included in sizeof, that is

1 struct node2 {3     int a;4     char c;5     int d[];6 };

The returned value is still 8.

8. Class Size:

(1). Empty class size. The instance of the null type does not contain any information. The size should be 0. however, when we declare an instance of this type, it must occupy a certain amount of space in the memory; otherwise, these instances cannot be used. The compiler determines how much memory is used. In g ++, each instance of the null type occupies 1 byte space. Note that empty struct is an empty class, which is why the empty struct of c ++ occupies 1 byte.

(2 ). when calling constructor, destructor, and member functions, you only need to know the function address. The address of these functions is related to the type, but not to the specific instance, therefore, no additional information is added to the instance.

(3) static data members are placed in global data members, which do not occupy the class instance size. Multiple class instances have only one entity. It can be considered as a special global variable.

(4). Non-static data members of the class are similar to those of struct, which also need to be aligned and may require byte filling.

(5 ). if a class contains a virtual function, this type generates a virtual function table and adds a pointer to the virtual function table in each instance of this type, therefore, a pointer must be added to the class size. For normal inheritance, the subclass and the base class share this pointer.

1 class A 2 {3 public: 4 static int a; 5 static char c; 6 A () {}; 7 ~ A () {}; 8 void foo () {}; 9}; 10 // the size of Class A is 1 byte, equal to the size of the null class, static data members a, c, and member functions do not occupy the class size. 11 12 class B13 {14 public: 15 int a; 16 char c; 17 A () {}; 18 ~ A () {}; 19 void foo () {}; 20}; 21 // the size of class B is 8 bytes 22 23 class C24 {25 public: 26 int; 27 char c; 28 A () {}; 29 ~ A () {}; 30 void foo () {}; 31 void virtual bar () {}; 32}; 33 // the size of class C is 12. The data member 8 is added with a pointer to the virtual function table.

 

(Thanks http://www.cnblogs.com/zhangyz/articles/4736758.html)

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.