sizeof C ++ in ()

[1] cattle off topic in the Visual C ++ and Mingw64 platform, short a [100], sizeof (a) What return?

A. 2  B. 4  C. 100  D. 200  E. 400

Answer: D 

Reference: https://www.nowcoder.com/test/question/done?tid=31013888&qid=1490#summary

Analytical: short int: 2 bytes [1.1]

Terms Values ​​expressed as follows return sizeof (in bytes)

  • Array - an array of the size of the space allocated compiling [1.1] (in particular, if the array is passed as a parameter to a function to do the sizeof () operation and the pointer arithmetic no difference) [1.2]
  • Pointer - storage space used by the pointer (storage address pointer length, long integer, should be 4) [2]
  • Type - the type of space occupied by [3]
  • Object - The actual size of the object space [4]
  • Function - the function's return type of occupied space. Return type of the function is not void [5]

[2-13] title

 1 char str[] = "Hello";
 2 char *p = str;
 3 int n = 10;
 4 sizeof(str) = 6;  // H,e,l,l,o,\0 [1.1]
 5 sizeof(p) = 4;    // [2]
 6 sizeof(n) = 4;    // [3]
 7 void Func(char str[10]){
 8     sizeof(str) = 4;      // [1.1]  
 9 }
10 void *p = malloc(100); 
11 sizeof(p) = 4;            // [2]

Title [2-14] using the sizeof () calculates the size of the space occupied by the object class,  a string alignment structure and principles class

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class A{
 5 public:
 6     int i;   //4
 7 };
 8 
 9 class B{
10 public:
11     char ch; //1
12 };
13 
14 class C{
15 public:
16     int i;    //4+1 ---> 对齐 4+4 = 8 
17     short j;
18 };
19 
20 class D{
21 public:
22     int i;    // 4+2+1 ---> 对齐 4+2+1+1 = 8
23     short j;
24     char ch;
25 };
26 
27 class E{
28 public:
29     int i;    // 4+4+2+1+1 = 12
30     int ii;
31     short j;
32     char ch;
33     char chr;
34 };
35 
36 class F{
37 public:
38     int i;    // 4+4+4+2+1+1 = 16
39     int ii;
40     int iii;
41     short j;
42     char ch;
43     char chr;
44 };
45 
46 int main(){
47     cout<<sizeof(A)<<' '<<sizeof(B)<<' '<<sizeof(C)<<' '<<sizeof(D)<<' '<<sizeof(E)<<' '<<sizeof(F)<<endl;
48 }

Answer: 41881216

Why byte alignment? In general, if the platform is not in accordance with the appropriate requirements for data storage are aligned, it will bring the loss of access efficiency. For example, some platforms are each read from the start even address, if an int in a place even address beginning, then a read cycle can be read out; and if the local store at the beginning of an odd address, you may need two read cycle, and the results of the two low bytes read were put together to get the int data type. Obviously, a lot of decline in the reading efficiency.

Byte aligned.

First address (1) the structure of the variable to be divisible by its broadest basic types of members Size

Each member (2) relative to the offset structure (offset) of the first address structure are multiples of the size of the member, the compiler may add padding bytes (internal adding) between members

The total size (3) structure to the structure type of the widest base of an integer multiple of the size of the members, the compiler after the last member of a stuffing byte (trailing padding)

  Spatial object class topic [2-15] using the sizeof () virtual function comprises calculating the magnitude

#include <iostream>
using namespace std;

class Base{
public:
    Base(int x):a(x){}
    void print(){cout<<"base:"<<endl;}
private:
    int a;
};

class Derived:public Base{
public:
    Derived(int x):Base(x-1),b(x){}
    void print(){cout<<"derived"<<endl;}
private:
    int b;
};

class A{
public:
    A(int x):a(x){}
    virtual void print(){cout<<"A"<<endl;}
private:
    int a;
};

class B: public A{
public:
    B(int x):A(x-1),b(x){}
    virtual void print(){cout<<"B"<<endl;}
private:
    int b;
};

int main(){
    Base obj1(1);
    Derived obj2(2);
    A obj3(3);
    B obj4(4);
    cout<<sizeof(obj1)<<' '<<sizeof(obj2)<<' '<<sizeof(obj3)<<' '<<sizeof(obj4)<<endl;
    return 0;
}

Answer: 48812 

Ordinary class member function does not take up memory, only the virtual function occupies a pointer to the size of the memory, because the system is a multi-use a pointer maintain virtual function table of the class, and pay attention to the virtual function regardless of how many virtual function how many entries (category contains contains ) will no longer affect the size of the class.

[2-16] entitled to use sizeof () to calculate the virtual space objects the size of the inherited class

 1 #include<iostream>
 2 using namespace std;
 3 class A{};
 4 class B{};
 5 class C:public A, public B{};
 6 class D:virtual public A{};
 7 class E:virtual public A,virtual public B{};
 8 class F{
 9 public:
10     int a;
11     static int b;
12 };
13 int F::b = 10;
14 int main(){
15     cout<<sizeof(A)<<" "<<sizeof(B)<<" "<<sizeof(C)<<" "<<sizeof(D)<<" "<<sizeof(E)<<" "<<sizeof(F)<<endl;
16     return 0;
17 }

Run Results: 111,484

Line 3, 4 -> A, B is empty class, the compiler will char to a placement empty class used to mark each of its object. Therefore its size is 1  

Line 5 -> multi-class inheritance, size 1

Line 6 -> virtual inheritance, the compiler for the class placed a pointer to the parent class, the compiler will not be placed char, pointer size of 4

Line 7 -> A and B virtual inheritance, pointing parent class A and class B pointer to the parent, combined size is 8 bytes

Line 8 -> F comprises a static member variable, this static instance space is not a member of the class, but as global variables in static memory, a shared instance of each class, and therefore having a size of 4 bytes .

 

Guess you like

Origin www.cnblogs.com/jg01/p/12404743.html