Back-end developers face questions

Language classes (C ++): ### Keywords action explanation:

 This study of the role of less volatile.

static

(1) modifying local variable

In general, for local variables are stored in the stack area, and local variables of the life cycle at the end of the statement block is executed will be the end. But if modified with static, then the variable will be stored in the static data area, its life cycle continues until the end of the implementation of the entire program. But here should be noted that, although after the local variable is modified with static, its life cycle and storage space has changed, but its scope has not changed, it is still a local variable scope is limited to the statement block .
 

(2) modify global variables

For a global variable, both can be accessed in the origin file can also be accessed (can only be declared with extern) in the other source files in the same project. Carried out with static global variable modified to alter the scope of its scope, visible to the whole project from the original source file is visible.

 

(3) modification function

With a static modification function, the situation is much the same with the modification of global variables is to change the scope of a function.

 

(4) C ++ static in

If the class is modified with a static function in C ++, it indicates that the function belongs to a class and not to any particular class objects; class if a modified static variable, this variable indicates the class All and all of its objects. They exist only in a copy storage space. Calls can go through the classes and objects.    

 

const meaning and implementation mechanism

const qualifier named constants to define a particular variable, to inform the compiler that the variable is not modified. Const habitual use, to avoid causing possible changes to certain variables should not be modified in the function.
 
(1) const modified basic data types

 Modified 1.const generally constant array and
  
 the basic data types, const modifiers can be used before the type specifier, can also be used in the type specifier, the result is the same. When using these constants, they do not change the values of these constants will be good. 
  
 2.const modified reference variable and pointer variable * &  
 
If asterisk * const is located on the left side, is used to modify the variable const pointer points to, i.e. the pointer to be constant;

If the const to the right of the asterisk, const pointer itself is modified, that is, the pointer itself is constant.

(2) const applied to the function,  

 1. As a const argument modifier
 
 function call time, with the corresponding variable initialization constant const, then the body of the function often performed according const quantizing the modified portion, the protective properties of the original object.
 [Note]: Parameter const argument is typically used when a pointer or reference; 
 
 2. the const function as a return value
 
 declared after the return value, const accordance with the "Principles modification" is modified, the corresponding play a protective effect.

(3) const usage in the class

You can not initialize const data members in the class declaration. Const implemented using the correct method: initialize const data member can only be initialized in the table class constructor
member functions of the class: A fun4 () const; the sense that it can not modify any type of variable where.

(4) const modified class object, the object defined constants 
const object can only call a constant function, other member functions can not be called.

external

In C, the extern modifier used before declaring a variable or function is used to illustrate "This variable / function is defined elsewhere herein to be referenced."

Macro definition and expansion, inline function difference

Inline function is the function code is inserted at the calling code. As #define macro, inline function calls by avoiding the overhead is to improve the efficiency, in particular, it can be by calling ( "Integrated Process") is optimizing compilers. Macro definition does not check function parameters, return values ​​or something, just start, relatively speaking, an inline function checks the argument types, it is more secure. Inline functions and macros are similar, but the difference is that the macro is replaced by the macro preprocessor, the inline function is achieved by control of the compiler. And an inline function is a real function, but they are needed, and inline functions like macros expansion, so cancel the push function parameters, reducing overhead calls.

Macro input is pre-compiler, then the result after macro expansion will be sent to the compiler to do the parsing. And macro functions, and so are at different levels, to operate different entities. Macro operations that token, the token can be replaced and other connecting operation, prior to parsing function. The function is the concept of language, will create the corresponding entity in the syntax tree, inline just a function of property.
For the question: With what functions they want to use? The answer is: a: The function can not completely replace macros, some macros may generate some variables in the current scope, functions can not. Two: inline function is just one of an inline function is to give a hint to the compiler, it is best to tell this function is called deployed at, and to cut the overhead of a function call (push, jump, return)

Inline function also has some limitations. Is the implementation of the code function can not be too much, if the body of the function inline function is too large, the general compiler will give up inline, while the ordinary way calling function. In this way, an inline function to normal function and efficiency

Inline function and the function body must be affirmed together to be effective.
 

### library routines: malloc, strcpy, strcmp achieve, commonly used library functions to achieve, which belong to high-risk function library functions

### STL principle and implementation: STL various types of container implementation, STL total of six components

The STL provides six components, can be applied in combination with each other:

1, containers (Containers): various data structures, such as: containers sequence vector, list, deque, associative containers set, map, multiset, multimap. Used to store data. From an implementation perspective, STL container is a class template.

2, algorithms (algorithms): various commonly used algorithms, such as: sort, search, copy, erase. From an implementation perspective, STL algorithm is a function template. Note that a problem: any of a STL algorithms are required to obtain a pair of iterators marked interval used to represent the operating range. This opening section is closed before the iterator marked interval, e.g. [first, last)

3, iterators (iterators): adhesive between the container and the algorithm, so-called "generic pointer." There are five types, as well as other derivatives change. From an implementation perspective, the iterator is an operator *, operator ->, operator ++, operator- - and other related operations overloaded pointer class template. All STL containers have their own exclusive iterator, only the container itself did not know how to traverse own elements. Pointer native (native pointer) is also an iterator.

4, imitation function (functors): behave like a function, can be used as a strategy algorithm (policy). From an implementation perspective, a functor overloaded operator () of class or class template. General function pointers may be regarded as narrow functor.

5, adapter (adapters): A method for modifying the container things, functor iterator interface. For example: STL provides queue and stack, though it seems container, but in fact can only be regarded as a container adapter, because they help the bottom completely deque, all operations by the underlying deque supply. Changing functors interfaces, known function adapter; container changing interfaces, known container Adapter; iterator changed interfaces, known iterator adapter.

6, configurator (allocators): responsible for space allocation and management. From an implementation perspective, a configuration is to achieve a dynamic configuration space, space management, the space freed class template.

Interactions of the six components: container (container) made by the allocator data storage space (configurator), algorithm (algorithm) to access the contents of container (container) by iterator (iterator), functor (functor) can assist algorithm ( algorithm) complete different strategy changes, adapter (adapter) can be modified or socket functor (functor)

Sequence containers:
Vector-array, the elements re-allocated is not enough memory, copying the original array elements allocated to the new array.
list- singly linked list.
deque- distribution map central controller (not container map), map recorded address of the array of fixed-length series. Remember this map is merely stored address of the array, the actual data stored in the array start .deque map center position (as deque, both before and after the element can be inserted) to find the address of an array, add data to the array, the array continue to look at the map of idle time is not enough to keep data array. When the map is not enough to re-allocate memory as a new map, new map the contents of the original copy of the map. Therefore, the use of greater than deque complexity vector, to make use of vector.

stack- deque based.
queue- deque based.
heap- complete binary tree, the maximum heap sort, store in the form of an array (vector) of.
priority_queue- heap-based.
slist- doubly linked list.

Associative containers:
SET, Map, multiset, multimap- red-black tree based (RB-tree), one kind of a binary search tree plus additional equilibrium conditions.

hash table- hash table. The data to be stored after the mapping function key into an index array (typically a vector), for example: the size of the index key% = array of array data (plain text by the algorithm may be converted to digital), and when the data this index for the array elements. Some key data after conversion algorithm may be the same array index value (collision problem, you can use linear probing, quadratic probing to resolve), STL is a method to solve the open chain, each element of an array to maintain a list, he put the data into the same index value of a list, so that when the list is relatively short delete, insert, search algorithms faster.

hash_map,hash_set,hash_multiset,hash_multimap-基于hashtable。

 

list and vector What is the difference?

vector has a contiguous memory space, support random access, if the need for efficient random access, but not with the insertion and deletion of efficiency, the use of vector.
list does not have a period of continuous memory space, it does not support random access, if a large number of insertions and deletions, not on random access, you should use the list.

 

### virtual functions: the role and realize the principle of virtual functions, what is virtual function, what role?

C ++ multi-state multi-state divided into static (compile-time polymorphism) polymorphism and dynamic (run-time polymorphism) two categories. Static polymorphism is achieved by overloading, templates; dynamic multi-state virtual function is through the protagonist of this article to be embodied.    
    
The principle of virtual functions: a virtual function table, virtual function pointers 

The role of virtual functions means: when a virtual function call, code is executed must match the type of call functions and dynamic objects. The compiler needs to do is how to provide efficient implementation of this feature. Different compiler implementation detail is not the same. Most compilers by vtbl (virtual table) and vptr (virtual table pointer) to achieve. When a class declares virtual functions or virtual functions inherited, this class will have its own vtbl. vtbl actually an array of function pointers, and some compiler uses is the list, but the methods are the same. vtbl Each array element corresponds to a function pointer to a virtual function of the class, and the class of each object will contain a vptr, vptr vtbl point of the address.

 

Each virtual function declared or inherited class virtual function, will have its own vtbl
while each object class will contain a pointer to the vtbl vptr to
virtual function table vtbl put in order according to their statement, the array vtbl each element corresponds to a function pointer to a virtual function in the class
if the subclass overrides virtual functions of the parent class, will be placed in its original position in the virtual table parent class virtual functions
in the case of multiple inheritance, each parent It has its own virtual table. Subclass member functions are placed in the first table of the parent class

 

Pure virtual function, why a pure virtual function?

Pure virtual function is declared in the base class virtual function, it is not defined in the base class, but requires that any derived class must define your own implementation. A method to achieve pure virtual function in the base class is added after the function prototype "= 0"

virtual void funtion1()=0

Reason:
1, in order to facilitate the use of multi-state properties, we often need to be defined in the base class virtual function.
2, in many cases, objects are generated base class itself unreasonable. For example, an animal as a base class can be derived tigers, peacock subclass of the animal itself generates a significant anomaly objects.

To solve the above problems, the concept of a pure virtual function, the function will be defined as a pure virtual functions (methods: virtual ReturnType Function () = 0;), requires the compiler must be overridden in a derived class to achieve polymorphism . Class also contains pure virtual function is referred to as an abstract class, it is not generated object. This solves both of these problems. It declares a pure virtual function of the class is an abstract class. Therefore, the user can not create an instance of the class, it can only create an instance of the derived class.

The purpose of defining pure virtual function in that the derived classes inherit just the interface functions.
Meaning pure virtual function, so that all class objects (mainly derived class objects) can perform actions pure virtual function, but the class can not provide a reasonable default implementation for the pure virtual functions. So pure virtual function declaration class is a subclass of the designer told, "You must provide an implementation of pure virtual functions, but I do not know how you will achieve it."

 

Why do we need virtual destructor, you do not need any time? Why destructor parent class to be defined as virtual.

When there is class inheritance will be declared virtual.

 

Inline functions, constructors, static member functions can be virtual function?

inline, static, constructor with three kinds of functions can not be virtual keyword.
inline is expanded at compile time, there must be an entity;
static belong to class of their own, must have a physical;
virtual function based on vtable (memory), constructor function if it is virtual, and when you call also need to find according to vtable, but the constructor is virtual in the case can not be found, because the constructor itself did not exist, can not create an instance of class, no instance, class members (except public static / protected static for friend class / functions, regardless of whether the remaining virtual) can not It has been visited.

Actually virtual function can not be inline: the price needed to run virtual functions mainly virtual functions can not be inline functions. It is also very well understood, because an inline function refers to at compile time instead of the command function call with the body of the function to be called itself, but the "virtual" virtual function means "to know until run time to call is which function. "but multi-state characteristics when running virtual functions is to know which specific virtual function call at runtime, so I can not inline function expansion at compile time. Of course, if the object directly through the virtual function call that can be inlined, but most virtual function is called by a pointer or reference object, such calls can not be inlined. Because this is a standard call invocation, the virtual function is not actually inlined.

Constructor can not be virtual. Further, virtual function is called in the constructor, the actual implementation of the corresponding parent class, because they are not well configured, the polymorphism is disable. 

Static objects belonging to the entire class, not in relation to an object, while it is also a function pointer storage member different from the general functions, which can not become an object virtual function pointers to the resulting dynamic mechanism.        

 

The constructor can call a virtual function?

Finally, to sum up some of the frequently asked questions about virtual functions:

1) virtual functions are dynamically bound, i.e., the virtual function pointer and the reference to find the actual function corresponding to the correct class, instead of performing the function defined classes. This is the basic function of a virtual function, it is not explained. 

2) constructor can not be virtual. Further, virtual function is called in the constructor, the actual implementation of the corresponding parent class, because they are not well configured, the polymorphism is disable. 

3) The destructor can be virtual, and, in a complex class structure, it is often necessary.
 
4) The function is defined as a pure virtual function, in fact, define the class as an abstract class, can not instantiate the object. 

5) is usually not pure virtual function definition body, but also can have.

6) The destructor may be pure imaginary, but pure virtual destructor definition body must have, because calling the destructor is implicit in subclasses. 

7) non-pure virtual function definition body must have, or be an error. 

8) override the derived class virtual function definitions must be exactly the same parent. In addition a special case, if the parent class return value is a pointer or reference, you can return the override this subclass pointer (or reference) is derived. In the example above, the definition of virtual Base * clone () in the Base; Derived may be defined as virtual Derived * clone (). We can see that this relaxation for Clone mode is very useful.

 

### Memory Allocation:

Memory allocation in three ways:

1) static storage area allocated. Memory in the program compile time has been allocated good, this memory during the entire operation of the program are present. Such as global variables, static variables.

(2) created on the stack. During a function, the function of the storage unit can create a local variable on the stack, the memory cells are automatically released at the end of the function execution. Stack memory allocation operation in the processor instruction set, high efficiency, but the limited amount of memory allocated.

(3) allocated from the heap, also known as dynamic memory allocation. Program at run time to apply any number of memory using malloc or new, the programmer is responsible when using free or delete release memory. Dynamic memory is determined by the lifetime of us, very flexible, but the problem is also up.

 

Principles of smart pointers

Smart pointer: refers to the actual behavior of the class object pointer like, it is a common implementation of the method reference count.
1. smart pointer will counter with a pointed object class associated with the class object reference count how many total tracks share the same pointer.
2. Each time when a new object class is created, and the reference count initialize the pointer is set to 1;
3. When an object is created as a copy of another object, with which corresponding reference copy constructor pointer and copy counts up;
4 when one object assignment, the assignment operator to reduce the left operand referents reference count (if the reference count goes to zero, to delete an object), and increases the reference count of the right operand referents; this is because the left pointer points to an object pointer points to the right, and therefore reference count + right pointer points to the object;
5. calls the destructor, a constructor reducing the reference count (if the reference count goes to 0, the deleted base object).
6. Implement smart pointers, there are two classic strategies: First, the introduction of helper classes, and second, use the handle class. Here to talk about the main method of introducing auxiliary class,

 

The difference between override and overload

the override (override) 
1, method name, arguments, return the same value.
2, subclass method can not be reduced access to the parent class method.
3, subclass method can not throw more exceptions than the parent class method (methods subclass but may not throw an exception).
4 exists between the parent and child classes.
5, is defined as a final method can not be rewritten.
overload (overload)
1, parameter types, the number, at least one sequence are not the same.  
2, can not override the return value only different method name.
3, present in the parent class and subclass of the same.

 

linux memory management mechanism, memory addressing, what is called virtual memory, memory paging algorithm, task scheduling algorithm,

Linux virtual memory to achieve the required six kinds of support mechanisms: address mapping mechanism, memory allocation recovery mechanism, cache and refresh mechanism, the requested page mechanism, exchange mechanisms and memory sharing mechanism

By the memory management program mapping mechanism to map the logical address to the physical address of the user program. When the user program is running, if the program does not use a virtual address corresponding to the physical memory found on the page request was required. If there is free memory available for allocation, it requests the allocation of memory (and therefore uses a memory allocation and recovery), and the physical page is in use is recorded in the cache (a cache mechanism used). If there is not enough memory to allocate, then call the exchange mechanism; free up part of the memory. In addition, in an address to map to find the physical page by TLB (translation back-up memory); exchange mechanism should be used to exchange cache and the physical page content exchange to exchange files, but also modify the page tables to map file location.
Between processes and threads, processes and threads of communication, that implement the principle of shared memory

 

Difference between the dynamic and static links

Dynamic links are established only a reference to the interface, with the true code and additional data stored in the executable module, then loaded at runtime; Static linking all the code and data are copied in this module, running when the library is no longer required.

 

Five kinds of I / O mode

Five kinds of I / O mode:
[1] blocking I / O (I / O operations are blocked by default in Linux I / O, that is, open and create a socket I / O is blocked I / O)
[2] Non-blocking I / O (O_NONBLOCK parameters may be used or when fcntl Open, fd is set non-blocking I / O)
[3] I / O multiplexer (I / O multiplexer, typically require non-blocking I / with the use of O)
[4] the drive signal I / O (the SIGIO)
[5] asynchronous I / O

 

2.2 select models

1. Maximum number of concurrent limit, because a process of the open FD (file descriptor) is limited, by the www.linuxidc.com FD_SETSIZE set, the default value is 1024/2048, the maximum number of concurrent Select appropriate model was limited. This change to change their own FD_SETSIZE? The idea is good, but take a look at it below ...

2. efficiency, select each call will scan all linear set of FD, so that efficiency will be presented linear decline, the consequences of FD_SETSIZE big change is that we are slowly, what? All overtime? ? ! !

3. kernel / user-space memory copy of the problem, how to tell the kernel to FD notifies the user space? In taking on this issue select memory copy method.

 

2.3 poll model

Are substantially the same efficiency and select, select disadvantages 2 and 3 do not get rid of it.

3. Epoll upgrade

The other model-by-criticism for a moment, look at the improvements Epoll at it, in fact, in turn, select the drawback that the advantages of the Epoll.

3.1. Epoll no limit on the maximum concurrent connections, the upper limit is the maximum number of files that can be opened, this number is generally much larger than the 2048 general this is a big number and relationship of system memory, a specific number can cat / proc / sys / fs / file -max look.

3.2. The efficiency gains, the biggest advantage is that it just you Epoll "active" connection, but nothing to do with the total number of connections, so in a real network environment, Epoll efficiency will be much higher than the select and poll.

3.3. Memory copy, Epoll use of the "shared memory" In this regard, the memory copy is omitted.

 

Realize the difference between the socket server and select the epoll (will ask)

Nature select to employ 32-bit integers 32, i.e. 32 * 32 = 1024 is identified, the value FD 1-1024. When the value fd of more than 1,024 limit, you must modify the FD_SETSIZE size. This time it can identify 32 * max range of fd.

For single-process multiple threads, each thread handle multiple cases of fd, select is not suitable.

1. All the threads are scanned from 1-32 * max, each thread processing are some fd value, this was a waste

Process 2.1024 ceiling, a treatment for multiple users, fd value is far greater than 1024

So this time should use the poll,

poll transmission is the length of the array header pointer and the array, as long as the length of the array is not very long, the performance is very good, because the poll single application in the kernel 4K (the size of a page to store FD), ideally controlled within 4K

One kind or poll epoll optimization does not need to traverse all the fd returns, maintain a list of fd in the kernel. select and poll list is maintained in the user mode kernel, and then passed to the kernel. But only 2.6 is only supported in the kernel.

epoll better suited to handle large amounts of fd, and active fd is not a lot of cases, after all, fd or more of a serial operation

 

epoll which trigger mode, what's the difference? (Must be very thorough explanation of the difference between the level trigger and edge-triggered and edge-triggered more confirmation of what to do in programming)

epoll can simultaneously support level trigger and edge trigger (Edge Triggered, just tell which file descriptors process has just become ready, only to say it again, if we do not take action, it will not be told again, this is called edge-triggered), edge-triggered theoretical performance is higher, but the code is quite complicated to achieve.

epoll file descriptor is also only inform those ready, and when we call epoll_wait () to get ready file descriptors, not the actual return of descriptor, but on behalf of a number of ready descriptors value, you just need to specify the epoll in order to obtain an array corresponding to the number of file descriptors, which also uses memory mapping (mmap) technology, thus completely eliminating the need for overhead copy of these file descriptors in the system call.

Another improvement is that the nature of the use of ready epoll event-based notification methods. After select / poll, the process only call a certain method, the kernel fishes monitor all file descriptors to scan, prior to registering a epoll file descriptor by epoll_ctl (), based on a file descriptor once ready , the kernel will use a callback mechanism similar callback quickly activate the file descriptor, when the process calls epoll_wait () will be notified.

 

## Network: TCP and UDP difference

key: TCP is a connection-oriented, reliable byte stream service

1. Facing the link: TCP for the link, use TCP connection-oriented means the two applications (usually a client and a server) must establish a TCP connection through the first three-way handshake before exchanging data with each other. In a TCP only two parties communicate with each other, it can not be used for multicast and broadcast TCP. UDP is not reliable transmission, you do not need to establish a link before transmission, multicast and broadcast applications can communicate one to many.

 
2. Reliability: TCP-end flow control, acknowledgment of data received and, using the timeout retransmission, the data out of order to ensure the reliability of data communication mechanism to reorder the like. The UDP is an unreliable service, the recipient may not receive the datagram sender.

 
3.TCP mode is a streaming protocol, UDP is a protocol datagram mode. Each output operation process are exactly generating a UDP datagram, and assembled into an IP datagram to be transmitted. All TCP data generated by the application with a single IP datagram sent may not have any real contact. TCP will stick pack and half a pack phenomenon.

 
4. Efficiency: speed, TCP generally slow, requires confirmation data transmission process, retransmission timeout, but also sort the data. UDP Without these mechanisms so fast. The proportion of data, TCP head of at least 20 bytes, UDP header of 8 bytes, a high relative efficiency. The assembly efficiency: TCP head of at least 20 bytes, UDP header 8 bytes, TCP is assembled on the system is relatively slow.

 
5. Uses: for TCP reliability, http, ftp used. And because UDP fast speed, video, online gaming and more with UDP, ensure real-time

 

TCP and UDP header bytes define

UDP and TCP three-way handshake and four waved status and message types

time_wait, close_wait state causes, keepalive

What is the sliding window retransmission timeout,

You know tcp listed options,

connect clog detection and prevention, readable socket under what circumstances?

connect clog, how to solve? (compulsory must ask)

 

## massive data processing: bitmap

Map-Reduce principle,

BloomFilter principle

 

 

 

 

 

Guess you like

Origin blog.csdn.net/wwxy1995/article/details/94485878