C ++ easy-to-use questions (Continuous updates) and incorrect questions
1. What is the output result of the following code?
12345678910111213141516 |
#include<stdio.h> char *myString() { char buffer[ 6 ] = { 0 }; char *s = "Hello World!" ; for ( int i = 0 ; i < sizeof(buffer) - 1 ; i++) { buffer[i] = *(s + i); } return buffer; } int main( int argc, char **argv) { printf( "%s\n" , myString()); return 0 ; } |
- Hello
- Hello World!
- Well
- All of the above are incorrect.
Resolution:
Answer: The D function char * myString () does not use new or malloc to allocate memory. The memory areas of all buffer arrays end with char * myString () in the stack area, stack memory is released, and the character array does not exist. Therefore, a wild pointer is generated, and the output result is unknown.
1234567 |
enum string{ x1, x2, x3= 10 , x4, x5, } x; |
Q: What is x equal? (0) in C language, this is generally a global variable, and the program will be cleared during initialization. 3. set the Class A, B, C, and D4 to the sequence of calling the Destructor A, B, C, and D in the program?
12345678 |
C c; void main() { A*pa= new A(); B b; static D d; delete pa; }
|
A B c da B d c (correct) a c d ba c B D analysis: the main knowledge points of this question are: global variables, static local variables, the heap allocation and stack allocation of the local variable space. The global variables and static local variables are divided from the static storage area. The difference between the two lies in the different scopes, the global variable scope is greater than the static local variable (only used to declare its function). The reason why D is released first is that C is released, in the program, the constructor of C is called first, and then the constructor of D is called. The Calling sequence of the constructor and the constructor is exactly the opposite. Local variable A is allocated from the heap space of the system through new. After the program runs, the system does not automatically recycle the space allocated to it. The programmer needs to manually call delete to release the space. The space of the local variable B object comes from the stack space of the system. After the method is executed, the system automatically releases the space by calling the destructor. The reason is that A is followed by B because B calls the Destructor only when the function is executed to the end "}", and the statement delete a is located before the end of the function. 4. If char is a byte, int Is 4 bytes, and pointer type is 4 bytes, the Code is as follows:
12345678910111213 |
class CTest { public : CTest():m_chData(‘\0’),m_nData(0) { } virtual void mem_fun(){} private : char m_chData; int m_nData; static char s_chData; }; char CTest::s_chData=’\0’; |
Q: (1) What is the value of sizeof (CTest) aligned in 4 bytes (12 )? (2) What is the value of sizeof (CTest) aligned in 1 byte (9 )? Select the correct answer. Resolution: the answer is 12 and 9, corresponding to the C ++ class memory layout. Refined image: A graph shows the space locations of member variables, member functions, static variables, and functions in the class. The theory is as above. The following is the execution result after the code is run. No pp, no truth. This is 4-byte alignment. The result is 12 = 4 (virtual table pointer) + 1 (char) + 3 (alignment complement) + 4 (int). The following values are 1-byte alignment, the result is 9 = 4 (virtual table pointer) + 1 (char) + 4 (int) 5.
1234567891011121314151617181920212223242526272829 |
#include<iostream> using namespace std; class MyClass { public : MyClass( int i = 0 ) { cout << i; } MyClass( const MyClass &x) { cout << 2 ; } MyClass &operator=( const MyClass &x) { cout << 3 ; return * this ; } ~MyClass() { cout << 4 ; } }; int main() { MyClass obj1( 1 ), obj2( 2 ); MyClass obj3 = obj1; return 0 ; } |
The output result is ()
- 11214444
- 11314444
- 122444 (correct)
- 123444
Resolution: The program contains three MyClass objects.
The first two objects are output at the time of construction.
The third object is MyClass obj3 = obj1 constructed in this way; here, the copy constructor is called and 2 is output.
Then, the three objects are analyzed in sequence, and the output is 444.
So the final output is 122444
MyClass obj3 = obj1; the copy constructor is called here.
If both obj3 and obj1 have been declared before, the coby assignment operator is called.