Telephone Question 1: virtual function calls in constructor and destructor; answer: virtual functions can be called in constructor and destructor, but virtual functions are statically bound at this time; instead of dynamic binding.
Telephone Question 2: Can an exception in C ++ be referenced? Answer: An exception can be a reference with high efficiency.
Telephone Question 3: What is close_wait in TCP status;
Answer: The close_wait status is a State of the passive closing party. It is a semi-closed state. The closed party receives the fin package and sends the ACK of the fin package, waiting for the upper-layer application to end the connection.
Telephone Question 4: time complexity of sorting algorithms; answer: nlogn is the best, and other online searches.
Interview question 1. write atoi functions. Answer: write your own atoi functions ---- (Note: when your own atoi function is the same as the atoi function in the database, throwing an exception will cause an exception to exit, I personally think that the exception is not unknown to be thrown by that function, so coredump)
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <assert.h>#include <iostream>#include <string>#include <exception>using namespace std; const unsigned int SIGN_BIT = 0x1 << 31; bool isDigit(const char ch){ if (ch <= '9' && ch >= '0') { return true; } return false;} int atoi_i(const char *str){ assert(str != NULL); while (' ' == *str){ str++; } int result = 0; bool signFlag = false; if ('+' == *str) { if (false == isDigit(*++str)) throw "input format error!"; } else if ('-' == *str) { if (false == isDigit(*++str)) throw "input format error!"; signFlag = true; } else if (*str > '9' || *str < '0') { throw "input format error!"; } do { result = result * 10 + *str++ - '0'; if ((result & SIGN_BIT) != 0) { throw "overflow error!"; } } while (isDigit(*str)); if (true == signFlag) { result = -result; } return result;} int main(int argc, char *argv[]){ char input[1024]; while (1) { try { cout << "Input Array:"; cin >> input; printf("exchange:%d/n", atoi_i(input)); } catch (const char *p) { cout <<"Error Info:" << p << endl; } catch ( ... ) { cout << "test" << endl; } } return 0;}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zhangxinrun/archive/2010/12/01/6048695.aspx
Interview question 2. sizeof and empty class; answer:
Class cbase
{
Int;
Char * P;
};
So what is output after running cout <"sizeof (cbase) =" <sizeof (cbase) <Endl?
This should be simple. The size of the two member variables -- 8.
Step 1: Empty class
Class cbase
{
};
Run cout <"sizeof (cbase) =" <sizeof (cbase) <Endl;
Sizeof (cbase) = 1;
In the deep exploration C ++ object model, this is said: it is a char inserted by the compiler, so that different objects of this class can be configured with unique addresses in the memory. That is to say, this char is used to identify different objects of the class.
Step 2:
Or the first class, run the result: sizeof (cbase) = 8
Step 3: Add a virtual function
Class cbase
{
Public:
Cbase (void );
Virtual ~ Cbase (void );
PRIVATE:
Int;
Char * P;
};
Re-run: sizeof (cbase) = 12
When there is a virtual function in the C ++ class, there is a pointer to the virtual function (vptr), and the pointer size is 4 bytes in the 32-bit system ". What about the inheritance class?
Step 4:
The base class is the above.
Class cChild:
Public cbase
{
Public:
CChild (void );
~ CChild (void );
PRIVATE:
Int B;
};
Run: cout <"sizeof (cChild) =" <sizeof (cChild) <Endl;
Output: sizeof (cChild) = 16;
The subclass size is the size of its member variables plus the subclass size.
Interview question 3. (1) objects can only be created on the stack, (2) objects can only be created on the stack;
Answer:
class HeapOnly { public: HeapOnly() { cout<<"constructor. "<<endl; } void destroy() { delete this; } private: ~HeapOnly(){} }; int main() { HeapOnly *p = new HeapOnly; p->destroy(); HeapOnly h; h.Output(); return 0; } #include <iostream> using namespace std; class StackOnly { public: StackOnly() { cout<<"constructor." <<endl; } ~StackOnly() { cout<<"destructor." <<endl; } private: void *operator new (size_t); }; int main() { StackOnly s; //okay StackOnly *p = new StackOnly; //wrong return 0; }
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zhangxinrun/archive/2010/12/03/6052551.aspx
Interview question 4. Search for a given number in a data group in ascending or descending order,
Personal thoughts: 1. determine the sequence form of the array based on the comparison of the beginning and end of the array; 2. Half-fold search algorithm.
Answer:
#include <stdio.h>#include <assert.h>using namespace std; static bool flag = true;bool intCompare(int value1, int value2){ return (value1 > value2) == flag;} int binary_search_i(int a[], int value, int start, int end){ if (start > end) return -1; int pos = (start + end)/ 2; if (value == a[pos]) { return pos; } else if (intCompare(value, a[pos])) { return binary_search_i(a, value, pos + 1, end); } else { return binary_search_i(a, value, start, pos - 1); }} int binary_search(int a[], int value, int n){ assert((a != NULL) && (n > 0)); if ((n == 1) || (a[0] == a[n - 1])) { if (a[0] == value) { return 0; } else { return -1; } } if (a[0] < a[n - 1]) { flag = true; } else { flag = false; } int temp = binary_search_i(a, value, 0, n - 1); while ((temp > 0) && (a[temp] == a[temp - 1])) { --temp; } return temp; } int main(){ //int a[] = {1, 3, 5, 7, 7, 7, 7, 7, 7, 7, 9, 10, 11}; int a[] = {11, 10, 9, 7, 7, 7, 7, 7, 5, 3, 1}; int arrayNum = sizeof(a) / sizeof(int); for(int i = 0; i < arrayNum; ++i) { printf("a[%d]=%d/t", i, a[i]); } printf("/n"); int value = 0; while(1) { printf("Input search value:"); scanf("%d", &value); printf("Pos in array:%d/n", binary_search(a, value, arrayNum)); } return 0;}
Interview question 5. Those algorithms are stable sorting and those algorithms are unstable sorting.
Answer: search online.