C++ Interview Questions (2)

11. Usage of sizeof

The role of sizeof is to return the number of bytes of memory occupied by an object or type.
1. For simple built-in types such as int, float, etc., their size is related to memory; for how many bytes they occupy in different machines, see Bytes
2. For structures and classes, they are stored in memory. The number of bytes occupied involves alignment issues: the
compiler processes the structure by default, so that the basic data types with a width of 2 are located at addresses that can be divisible by 2, and the data types with a width of 4 are located at addresses that can be divided by 4. Divide the address; in this way, padding bytes may be required between the two numbers, and the sizeof value of the entire structure will increase. In general, byte alignment satisfies three principles:
a. The first address of a structure variable can be divisible by the size of its widest basic type member
b. The offset of each member of the structure relative to the first address of the structure is the same It is an integer multiple of the member size
c. The total size of the structure is an integer multiple of the size of the widest basic type member of the structure. The sizeof value of
an empty structure is 1, and the compiler allocates one byte of space for empty classes and structures. 3. The sizeof
of the union is the maximum sizeof of each member
4. The sizeof of the array is the number of bytes of memory occupied by the array. When the array is a formal parameter, the value of sizeof is equivalent to the sizeof of the pointer
5 The sizeof of the pointer is equal to the width of the computer's internal address bus (4 for a 32-bit machine), regardless of the object it refers to.
6. The sizeof of a function is the size of the function's return value, and the function itself is not called; The size of an empty function cannot be evaluated only by the function name. The function with parameters must be written in the actual parameter table
. 7. The influencing factors of the sizeof of the class are:
a> The size of the non-static data member, the static member is in the static storage area The memory space is allocated, and the objects of the class are allocated on the heap, so when calculating the size, static members are not calculated.
b> the order of data members, the reason why the order of data members affects the size of the class is because the alignment is different, for example , the int a; short b; char csize after alignment is 8, and the size after short b; int a; char calignment is 12
c> byte correction and alignment, same as above
d> base class Size, the size of the subclass is affected by the parent class, because the subclass inherits the members of the parent class
e> whether there is a virtual function, if there is a virtual function in the class, it will increase by 4 (32 bits)/8 (64 bits) bytes, No matter how many virtual functions (either own or inherited) there are, only 4 (32-bit)/8 (64-bit) bytes are added; because there is only one virtual function table.
f> the compiler used
g> inheritance model, whether it is virtual inheritance, in the case of virtual inheritance, an additional virtual pointer (4 (32-bit)/8 (64-bit)) should be added, but also need to pay attention to a function There is only one virtual pointer of its own; that is to say, if B virtual inherits A, C virtual inherits A, D inherits B, and C is only counted as the size of a virtual pointer; if B virtual inherits A, C virtual inherits D, and E inherits B, C needs to calculate two virtual pointers.

example:

class A {}; sizeof(A) = 1;
class A {virtual fun()}; sizeof(A) = 4(32位)/8(64位)
class A {static int a;}; sizeof(A) = 1;
class A {int a}; sizeof(A) = 4;
class A {static int a; int b;}; sizeof(A) = 4

12. Introduction to C++ STL

STL is an implementation of generic programming ideas, broadly divided into three categories: algorithms, containers, and iterators. The algorithm part mainly consists of <algorithm>、<numeric>、<functional>components and implements commonly used algorithms; the container mainly implements some data structures such as vectors, lists, etc. The iterator is a data type that allows programmers to check the elements in the container and implement element traversal; C++ is a data type for each A standard container defines an iterator type that provides a more general approach than subscripting.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324737377&siteId=291194637