1. Comparison of output results
1)
Output results: 21
2)
Output Result: 12. Although the loop was only made once, but right! X + + is a self-adding operation.
2. Pointer arithmetic
Output 8,8. When the program runs, the printf statement reads from right to left, and PTR points to 8. Because * and + + have the same priority, the binding order is from right to left, so *ptr++ is equivalent to * (ptr++).
3. Operator Precedence
4. Using bit operations to achieve the addition of two integers, please use code to implement
5. Give three integers a, B, C, function implementation to take three number of intermediate number, can not use sort, integer operation
As little as possible.
6. How do I exchange the values of a, B, and do not use any intermediate variables?
Bitwise XOR, don't worry about hyper-bounds.
7. The following switch statement outputs what.
Break and default are not considered, starting with C execution to the end. Output CD.
8. What is the result of the following code output?
Parsing: The sentence of the macro was replaced by the preprocessor as the result of the:*&array[5]-4=6; operation 2=6;
Because the minus sign is higher than the assignment priority, the minus sign is processed first, since the minus sign returns a number instead of a valid value, the
To compile the error.
Answer: The program compiles correctly, but the runtime crashes
8.1. In the use of simple macro definitions, it is easy to misunderstand and misuse when the replacement text represents a string that is an expression. The following example:
Example 1 #define N
void Main ()
{
int a=n*n;
printf ("%d", a);
}
The output is 8. A=2+2*2+2=8.
9. Write a "standard" macro min, this macro enters two parameters and returns the smaller one.
10. Which of the following const is superfluous?
In the subject, option a modifies the value of the function to return the pointer to the byte-type value that is constant;
A function of the kind is a constant member function. The constant member function can be understood as a "read-only" function, which cannot change data members
, and cannot invoke member functions that can cause changes in the value of the data member, only the const member function, which will not
The function that modifies the data member GetBuffer is declared as a const type. This greatly improves the robustness of the program. D is redundant because the const-decorated pointer needs to be initialized when it is defined. Note: (The const int*a is equivalent to the int const *a).
What is the difference between 11.const and # define?
The C + + language can define constants with const, or you can define constants in # #, but the former has more
Many advantages:
Const constants have data types, and macro constants do not have data types. The compiler can perform type safety checks on the former
Only character substitution, no type security checks, and may not be expected in character substitution
The error (marginal effect).
Some integrated debugging tools can debug const constants, but cannot debug macro constants. In
Only const constants are used in C + + programs without using macro constants, that is, const constants completely supersede macro constants.
The use of 12.sizeof.
The output result is 4,11,100,400,4,3,4,6,8,4,4,. The result of size (*Q3) is 1,a is the first letter.
SS1 is a character pointer, and the size of the pointer is a fixed value, which is 4 bytes, so sizeof (SS1) is 4 bytes.
SS2 is an array of characters, which is initially undefined and is determined by the specific padding value. The padding value is "0123456789". The space occupied by 1 characters is 1 bytes, 10 is 10 bytes, plus the implied "\", so it is 11 bytes altogether.
SS3 is also a character array, which starts with a pre-allocation of 100, so its size is 100 bytes altogether.
SS4 is also an array of integers, which begins to pre-allocate 100, but the space occupied by each integer variable is 4, so
Its size is altogether 400 bytes.
Q1 is similar to SS2, so it is 4 bytes.
Q2 there is a "\ n", "\ n" is counted as one, so its space size is 3 bytes.
Q3 is a character pointer, the size of the pointer is a fixed value, is 4, so sizeof (Q3) is 4 bytes.
A and B are two structural bodies. By default, in order to facilitate access and management of elements within the structure, the structure
When the length of the element in the body is less than the number of bits of the processor, it is aligned with the longest data element within the structure.
Bits, that is, the length of the structure must be an integer multiple of the longest data element. If the structure body exists longer than
The number of processor bits, then the number of bits in the processor is aligned to the unit. But continuous elements of the same type within the structure
will be in contiguous space, as well as in arrays.
There are 3 short type variables in struct A, each aligned in 2 byte, and the struct alignment parameter is in the default 8-byte pair
, A1, A2, A3 are 2-byte aligned, sizeof (A) is 6, and it is an integer multiple of 2. The A1 in B is 4-byte aligned, A2
For 2-byte alignment, the struct default alignment parameter is 8, then the A1 takes 4-byte alignment, A2 2-byte alignment, and the struct size is
6 bytes, 6 is not an integer multiple of 4, the fill empty byte, increases to 8 o'clock, meets all the conditions, then sizeof (B) is 8.
13.sizeof usage 2.
Output result: Answer: 4,8,8,12,24.
Because static variables are stored in the global data area, and sizeof calculates the size allocated in the stack, it is not accounting
Count, so sizeof (A1) is 4.
To take care of the data alignment, the int size is 4,char size 1, so sizeof (A2) is 8.
To take care of the data alignment, the float size is 4,char size 1, so sizeof (A3) is 8.
In order to take care of the data alignment, the float size for 4,int size is 4,char size of 1, so sizeof (A4) is 12.
In order to take care of the data alignment, the double size of 8,float size for 4,int size is 4,char size of 1, so
sizeof (A5) is 32.
14. Describe the difference between sizeof and strlen.
sizeof (SS) results for 400,SS, which represents the size in memory, 100x4.
STRLEN (ss) error, the strlen parameter can only be char* and must end with "\".
Cout<<sizeof (X) <<endl; result is 12, memory is padded.
Cout<<sizeof (x) <<endl; results 12 for the same reason.
Through the deep understanding of sizeof and strlen, the difference is as follows:
(1) The result type of the sizeof operator is size_t, and its typedef in the header file is the unsigned int type. The class
Type ensures that the byte size of the largest object to be built is accommodated.
(2) sizeof is an operator and strlen is a function.
(3) sizeof can use the type parameter, strlen can only use char* to do parameters, and must be "" to end.
sizeof can also use functions to make arguments, such as:
The result of the output is sizeof (short), which is 2.
(4) The parameters of the array do sizeof do not degenerate, passed to the strlen is degraded to the pointer.
(5) Most compilers have calculated sizeof at compile time, which is the length of the type or variable.
(6) The result of Strlen is calculated at run time to calculate the length of the string, not the type
The size of the memory.
(7) After sizeof if the type must be parentheses, if the variable name can be no parentheses. This is because sizeof is
operator instead of a function.
(8) When a struct type or variable is used, sizeof returns the actual size. When using a static number of spaces
Group, sizeof returns the dimensions of all arrays. The sizeof operator cannot return a dynamically allocated array or an outer array.
Size.
(9) The array is passed as a parameter to the function simultaneous is a pointer instead of an array, passing the first address of the array, as
Fun (char [8]), Fun (char []) are all equivalent to fun (char *).
(10) to calculate the size of the structure variable, the data alignment problem must be discussed. In order to make the CPU access the fastest (this
With the CPU to take the number of operations, the detailed introduction can refer to some computer principles of the book, C + + when processing data
The size of a member in a struct variable is often calculated as a multiple of 4 or 8, which is called data alignment.
Doing so may waste some memory, but in theory the CPU is faster. Of course, such a setup will read and write some
Application to generate data files or Exchange data for inconvenience. MS VC + + in the alignment settings, sometimes sizeof
are not equal to the actual. Generally in VC + + #pragma pack (n) settings can be. Or if you want to save by byte
Without data alignment, you can modify the "data" in the Advanced Compiler tab in the Options dialog box
Alignment "is aligned by byte.
The sizeof operator cannot be used for a function type, an incomplete type, or a bit field. An incomplete type refers to an unknown
The data type that stores the size data, such as an array type of unknown storage size, structure or union type of unknown content, void
Type, and so on.
use sizeof for the function, which is replaced by the type of the function return value during the compilation phase. such as: int F1 () {return
0;}.
(13) The size of the array is the product of the number of dimensions x the size of the array element.
15. Find the error in the program below and explain why it is wrong.
sizeof (STRARR1) is a 12,sizeof (string) of 4. sizeof (PSTRARR1) is 4. In order to be fully displayed, it should be changed to J<sizeof (*PSTRARR1) *2/sizeof (String).
16. Write down the following sizeof answer.
The size of the class base. Because the class base has only one pointer, the size of the class base is 4. Derive size
Similar to base, so it is also 4. Aa.
17. What is the result of the output below?
Because var[] is equivalent to *var, it has degenerated into a pointer, so the size is 4.
Float f accounts for 4 bytes, char p is 1 bytes, int adf[3] occupies 12 bytes, total 17 bytes. According to the internal
To select a multiple of 4, which is 20 bytes.
18. How much space does an empty class occupy? What about the empty classes of multiple inheritance?
This indicates that the empty class occupies 1 space, and the single-Inheritance empty class space is also 1.
The empty class space for multiple inheritance is still 1. However, virtual inheritance involves virtual tables (virtual pointers), so the size of sizeof (C) is 4.
C + + written classical procedure (i)