When we need to recruit a C + + engineer, I will generally briefly ask the basic concepts of C + +. For example: The role of Static, the role of const and so on. After answering these basic concepts, I'll ask a few questions about the object model to assess the level.
On several occasions the whim asked a few questions about sizeof.
Class A{};sizeof (A) =?
This problem as long as the people who have known is equal to 1, if the answer 0, then you may be a C + + soon after the beginning of the people, follow-up will not ask more in-depth questions.
This problem leads to other problems, such as how much space the member functions occupy, how much space the member variables occupy, and how much space will be occupied by the virtual function. Virtual inheritance, defining optimizations for classes, and so on. These questions will be covered in a series of subsequent articles.
Let's start with an introductory, member function and member variable space-occupying problem.
If you add a member function to the class above, the result of sizeof is still 1, which means that the member function does not introduce additional space consumption.
If you add a member variable to the class above, the result of sizeof will vary depending on the type of variable.
If you add more than one member variable to the above class, the results of sizeof may vary depending on the order.
First of all, when there are no member variables, the result of sizeof is 1 instead of 0.
- The compiler did not raise questions at compile time this hollow class is not allowed to exist
- When invoking the new method, the compiler will do two things, allocate memory and call the constructor (the compiler generates the necessary constructors for this class, as described in the subsequent series).
This is the question, how much memory space do you need to allocate? If it is 0, then is it supposed to allocate a space of size 0? This makes the compiler too difficult. How can you allocate space to 0, and then you can guarantee the uniqueness of the object? (unique address) that is still the default to it 1, so I can work normally, more and waste. This is why you need to give the empty class the default 1 space.
Why does the member function not occupy memory space? (non-virtual function)
- In fact, the member function takes up memory space.
- The member functions used by multiple objects are the same (non-virtual functions, so the code executed is the same)
- The system allocates memory for member functions, but all objects are shared.
- The compiler automatically adds the this pointer to the member function where it is compiled to the calling member function, so that each object instance invokes a member function with different arguments
The system only needs to keep one copy of the memory for this non-virtual function, without having to allocate memory for each object instance to waste space.
Because the different types require different spaces, the type of the member variable causes the space to occupy differently.
Why can the order affect the size of a class when there are multiple member variables?
Want to see an example
Class a1{ char C1; int i; Char C2; };class a2{ Char C1; char C2; int i;}; Class a3{ int i; Char C1; char C2;};
The top three different classes, whose member variables are the same, let's see how the results are:
sizeof (A1) = 12; sizeof (A2) = sizeof (A3) = 8.
Why not 6? Why is the size of the first class 12? Why are the second and third 8?
- The size of Char is 1
- Default by 4-bit alignment, less than 4 bits, 0 alignment
- int under 32 bits is 4
C + + object Model (1)