"Programmer's Book" reading notes

1. i+++j combine from left to right

2. The data of unsigned int type and the data of int type are automatically converted into unsigned int.
  When performing integer promotion, char, signed char, unsigned char, and short int are automatically converted to int.

3. In a C++ program, when calling the function compiled by the C compiler, why add extend "C"

  C++ supports function overloading, C does not support function overloading. The name of the function in the library after C++ is compiled is different from that in the C language. Suppose the prototype of a function is
  void foo(int x, int y), and the name in the library after being compiled by the C compiler is _foo, while the C++ compiler will generate names such as _foo_int_int.
  C++ provides the C connection exchange specified symbol extend "C" to solve the name matching problem.

4. The role of indef/define/endif in header files: to prevent header files from being repeatedly referenced

5. The difference between #include<filename.h> and #include "filename.h"
#include<filename.h> When retrieving the header file, the system will first look for filename.h from the system standard library path, and then look elsewhere. Faster for system files.
#include"filename.h" When the system retrieves the header file, it starts from the directory where the program is located, that is, the user's working path. Faster for custom files.

6. How to judge whether a program is compiled in C or C++?
Compiler type, just judge _cplusplus or _STDC_ macro
. Simply put, because C language does not have the concept of overloaded functions, all functions in the program compiled by the C compiler have only the entry corresponding to the function name. And because the C++ language has the concept of overloaded functions, if only the function name corresponds to the entry, the line will be confused. Therefore, the program compiled by the C++ compiler should be the function name + parameter type list corresponding to the entry. 
  Note that because the main function is the entry point of the entire program, main cannot be overloaded. Therefore, if a program has only the main function, it is impossible to confirm whether it was compiled by a C or C++ compiler. 
  You can view the function name entry through nm. 
  For example, a function 
  int foo(int i,float j) 
  C compiled program can view 
  f through nm 0x567xxxxxx (address) 
  C++ compiled program, through nm 
  f(int,float) 0x567xxxxxx

7. After the main function is executed, is it possible to execute a piece of code? If you need to add a piece of code that is executed after main exits, you can use the atexit() function to register a function.

8. Use the preprocessing command #define to define how many seconds in a year
  #define MIUNTES_PER_YEAR=(365*24*60*60)UL unsigned long integer

9. Write a macro that outputs the smaller of the two parameters with #define MIN(A,B) ((A<=B)?A :B)

10. Use of const in C: You can define const constants, limiting a variable that is not allowed to be changed. Using const can improve the robustness of the program to a certain extent; modify the parameters of the function, the return value and even the definition body of the function

11. What is the difference between const and #define:
   Both can define constants, but the former has more advantages: const constants have data types, while #define has no data types, the compiler can perform safety inspection on the former, and just replace the latter, There are no type safety checks, and unexpected errors can occur in character substitution; some debugging tools can debug the former, but not macro constants

12.sizeof calculates the number of bytes.
The size of the string pointer is 4; there is an implicit "\0" behind the string array; in order to facilitate the access and management of elements in the structure, when the length of the elements in the structure is less than the number of bits of the processor, the number of bits in the structure is used. The longest data element is its unit,
that is, the length of the structure must be an integer multiple of the longest data element; if there is an element in the structure whose length is greater than the number of processor bits, the number of bits of the processor is used as the alignment unit.

Data alignment: The memory address where the data resides must be an integer multiple of the data length.

For a class, even if it is empty, the number of bytes is still 1; the size of the multiple inheritance empty class is still 1, and the number of bytes of the pointer is 4;

13. In-depth understanding of sizeof and strlen:
sizeof is the maximum byte size of the created object; sizeof is an operator, while strlen is a function; sizeof can use types as parameters, while strlen can only use Char* as parameters, and Must end with "\0". strlen is calculated at runtime and is
used to calculate the length of the string and the size of the memory occupied by the type. The type after sizeof needs to be parenthesized;

Applicable occasions of sizeof: 1. One of the main uses of sizeof is to communicate with routines such as storage allocation and I/O systems 2. Use sizeof to view the unit bytes occupied by a certain type of object in memory,

3. When dynamically memory allocation objects, you can let the system know how much memory to allocate.

4. Facilitates some types of extensions. Many struct types in Windows have a dedicated field to store the size in bytes of the type.

5. Since the number of bytes of the operand may vary in implementation, it is recommended to use sizeof instead of constant calculations when the byte size of the operand is involved.

6. If the operand is an array parameter in a function or a parameter of a function type, sizeof gives the size of its pointer.


14. The difference between inline functions and macros:
Compared with ordinary functions, inline functions can speed up the running of the program, because there is no need to interrupt the call, and inline functions can be directly embedded into the object code during compilation, while macros are just simple replacement. Inline functions are subject to parameter type checking, macros do not.


Inline function usage: a function is called repeatedly; the function is only a few lines, and the function does not include statements such as for while switch.
Generally speaking, inline functions are not used to write small programs, but to complete a project, when a simple function is called multiple times, it can be written as an inline function.

Macros are important in C, but rarely used in C++; one of the rules of macros is: you should never use them unless you have to.

Macros are not functions, they just replace the relevant strings in the program with macros before compiling. Inline functions are functions that do not generate code separately, but embed the relevant code at the call site.


15. The difference between a pointer and a reference:
1. From the perspective of the phenomenon: the pointer can change the value it points to at runtime, while the reference will not change once it is bound to an object
2. From the perspective of memory allocation: the program Allocate a memory area for a pointer variable, while the reference does not allocate a memory area
3. From a compilation point of view: the program adds the pointer and the reference to the symbol table respectively when compiling, and the variable name and the address corresponding to the variable are recorded in the symbol table. The address value corresponding to the pointer variable in the symbol table is the address value of the pointer variable, and the address value corresponding to the reference in the symbol table is the address value of the reference object. After the symbol table is generated, it will not be changed, so the pointer can change the object pointed to (the value in the pointer variable can be changed), but the reference object cannot be changed.
4. Non-null difference: A reference cannot be null under any circumstances, and a reference must always point to some object. 5. Legality difference
: references do not need to be checked for legality; but pointers are always tested to prevent nulls
Can not be modified.


16.This pointer is generated after instantiating an object and points to itself. The characteristics of the this pointer in the class can be clearly declared in the member function
:
(1) The this pointer can only be used in member functions, and cannot be used in global functions and static functions. The default first parameter of the member function is T* const this
(2) The this pointer is constructed before the start of the member function and cleared after the end. This lifetime is the same as any function parameter.
(3) This pointer is placed in different locations due to different compilers. Stacks, registers, global variables are all possible.
(4) Most compilers pass pointers through the ecx register. Generally speaking, different compilers will follow the unified parameter passing rules, otherwise the obj generated by different compilers will not match.
     Before calling call, the compiler puts the address of the object into ecx, and this is passed as the first parameter of the function parameter. The this pointer is generated before the call. When a class is instantiated, it only allocates space for variables in the class and
     does not allocate space for functions.
(5) There is only one difference between a class and a structure in C++: the members of a class are private by default, and the default of a structure is public
(6) the this pointer is defined in the member function. After obtaining an object, the this pointer cannot be used through the object. . Therefore there is no way to know the address of an object's this pointer. in member functions.
(7) Ordinary functions (member functions, static functions) do not create function tables to hold function pointers. Only virtual functions are placed in the function table. But even if it is a virtual function, if the compiler knows which function is called, the
    compiler will not call it through the pointer in the function table, but call the function directly.
(8) It is not allowed to assign addresses to pointers such as int* ptr;ptr=(int *)OX800


17. Write out function pointers, function return pointers, const pointers, pointers to const, const pointers to const
 void (*f)(), void* f(), const int*, int* const, const int* const

18. The lost pointer is also called a floating pointer or a runaway pointer. It is generated when a pointer is deleted and the memory it points to is released without setting it to empty.

19.C++ has malloc/free, why do you need new/delete?
   The former are standard library functions and the latter are operators. Both are used to dynamically allocate and release memory.
   For non-internal data types, the former alone cannot meet the requirements of dynamic objects. Objects automatically execute constructors when they are created, and automatically execute destructors before they die.
  Since the former is a library function and not within the control of the compiler, the task of executing the constructor and destructor cannot be imposed on the two. Hence the need for the latter.

20. What is the difference and connection between pointer and handle?
   The handle is a 32-bit integer, which is actually an integer index into a list of physical addresses in memory of an object (window, etc.) maintained by Windows in memory. Because Windows memory management often releases the memory of the currently free object, and resubmits the access to the physical memory when needed, the physical address of the object is changed, and the program is not allowed to access the object directly through the physical address. The program passes the handle of the object it wants to access to the system, and the system retrieves the list of objects it maintains according to the handle to know the object the program wants to access and its physical address.
   A handle is a pointer to a pointer. We know that the so-called pointer is a memory address. After the application starts, the objects that make up the program are resident in memory. If we understand it simply, it seems that we only need to know the first address of this memory, then we can use this address to access objects at any time. But if you really think so, you are dead wrong. We know that Windows is an operating system based on virtual memory. In this system environment, the Windows memory manager often moves objects back and forth in memory, one at a time to meet the memory needs of various applications. An object being moved means its address has changed. If the address always changes like this, where do we go to find the object? In order to solve this problem, the Windows operating system frees up some memory addresses for each application program to specifically register the address change of each application object in the memory, and this address (the location of the storage unit) itself is unchanged. After the Win memory manager moves the object's location in memory, it informs the handle address of the object's new address to save it. In this way, we only need to remember the handle address to indirectly know where the object is in memory. This address is allocated by the system when the object is loaded and released to the system when the system is unloaded. Handle address (stable) -> record the address of the object in memory -> the address of the object in memory (unstable) -> the actual object. However, it must be noted that each time the program is restarted, the system cannot guarantee that the handle assigned to the program is still the original handle, and in most cases it is indeed different. If we regard entering a movie theater to watch a movie as the startup and operation of an application, then the handle assigned by the system to the application is always different, which is the same as the tickets sold to us by the movie theater are always different seats each time.

       HDC is the device description table handle.

       CDC is the device description table class.

       Can be converted to each other with GetSafeHwnd and FromHandle.

       Handles and pointers are actually two completely different concepts. The Windows system uses handles to mark system resources, and uses handles to hide system information. You just need to know that there is this thing, and then call it, it is a 32bit uint. A pointer marks a physical memory address, which is a different concept


21. Recursive pages 96, 98

------------------------------------------------------------

The unit of information transmitted in the interaction of the peer entities again is the protocol control unit; it is divided into control information and user information.
The service access node of the network layer is called the network address, including the network number and the host address.

What is the difference between switching and routing? Features of VLAN:

Routing and switches are the two pieces of hardware we see most on the network. A router is a hardware device at the Internet layer, and its main task is to process IP packets and use IP addresses for routing, while a switch is a hardware device at the link layer, which mainly processes frames and exchanges information through the physical addresses of the frames. and choose which network to send the message to. In fact, from a conceptual point of view, the difference is that one is route selection through IP, and the other is route selection through physical address. People usually use HUB (hub) to compare with switches, and the biggest difference between switches and HUBs is that they can isolate conflicts. domain, this process must be routed to the physical address.

Switching refers to forwarding and filtering detection, which is the work of switches, and it is at Layer 2, while routing refers to non-direct links in network lines, which is the work of routers and is at Layer 3. There are many differences between switching and routing. First, switching does not require IP, but routing does, because IP is a Layer 3 protocol, and Layer 2 requires a MAC address. Furthermore, Layer 2 technology is different from Layer 3. Layer 3 can do VLAN, port binding, etc., Layer 3 can do NAT, ACL, QOS, etc.
    VLAN is the English abbreviation of virtual local area network, it is a pure two-layer technology, it has three characteristics: control broadcast, security, flexibility and scalability.


Before an IP packet reaches a destination, it may be called fragmented but not reassembled. In the TCP/IP protocol, packet loss may occur when the network is congested.

File transfer is based on the TCP protocol. 254 hosts for Class C URLs. Ethernet translation control packets are based on the destination IP address.

The TCP server crashes before the data is sent, and the TCP stack returns a SYN.

Configure IP command in Win2000 system: ipconfig; test whether the machine is installed with TCP/IP protocol command: ping 127.0.0.1; list the current connection command of this machine: netstat -a

What is the SNMP protocol? What are the characteristics?
Simple Network Management Protocol (SNMP: Simple Network Management Protocol) is a set of network management protocols defined by the Internet Engineering Task Force (IETF: Internet Engineering Task Force). The protocol is based on Simple Gateway Monitor Protocol (SGMP: Simple Gateway Monitor Protocol). Using SNMP, a management workstation can remotely manage all network devices that support this protocol, including monitoring network status, modifying network device configurations, and receiving network event warnings. Although SNMP was originally oriented towards IP-based network management, it has also been successfully used as an industry standard for telephone network management.

Features: easy to implement; open and free; detailed information; can be used to control various equipment.

 

Does the SNMP protocol require a dedicated connection? None

What is the difference between a stored procedure and a function: a stored procedure is a collection of user-defined SQL statements, designed for a specific table, users can call the stored procedure, a function is a method that has been defined by the database, receives parameters and returns a value of a certain type, No specific table is involved.

The role of the cursor: used to locate the rows of the result set.
How to know the cursor is at the end: By judging the global variable @@FETCH_STATUS, it can be judged whether it is at the end, usually this variable is not equal to 0, it means an error or at the end

Triggers are divided into pre-trigger and post-trigger. The difference between the two triggers is that the pre-trigger runs before the triggering event occurs, while the post-trigger runs after the triggering event occurs. Usually pre-triggers can get the previous and new field values ​​of the event.

What is the difference between statement-level triggering and row-level triggering: Statement-level triggers can execute before or after the statement execution, while row-level triggering fires once for each row affected by the trigger.

SQL injection attack: The attacker inserts the SQl command into the input of the web form or the query string of the page request to deceive the server to execute the SQL command.

SQL topics starting from P286 should be read
----------------------------------------- -------------------
1. The difference between a process and a thread:
a process is an execution of a program, and a thread can be understood as a program fragment executed in a process. In a multitasking environment, the difference between the two is even greater: the
processes are independent, which is reflected in the memory space and context environment; threads run in the process space.
Generally speaking, a process cannot break through the boundaries of the process to access the storage space in other processes; because the thread is in the process space,
the threads in the unified process share the same memory space
and the codes at both ends of the unified process cannot be executed at the same time; the
thread belongs to the process , when the process exits, the threads spawned by the process are also forced to exit and clear.
Threads that take up less resources than processes can have priority.
A process is a running activity of a program with a certain independent function on a certain data set, and a process is an independent unit of the system for resource allocation and scheduling.

A thread is an entity of a process and the basic unit of CPU scheduling and dispatching. It is a basic unit smaller than a process that can run independently. Threads basically do not own system resources themselves, but only have a few resources that are essential in operation. (such as the program counter, a set of registers and the stack), but it can share all the resources owned by the process with other threads belonging to the same process.

A thread can create and revoke another thread; multiple threads in the same process can execute concurrently

2. How to realize the communication between processes?
The most common methods of process communication are signals, semaphores, message queues, and shared memory.
Signals and semaphores have different mechanisms, although both can achieve synchronization and mutual exclusion. The former is implemented using a signal processor, and the latter is implemented by P and V operations.
Message queues are an advanced form of communication. It can be shared by multiple processes. Too many messages in one process can also be placed in multiple message queues.

3. In the network programming and design of concurrent servers, the difference between multi-threading and multi-process:
each process has its own address space in multi-process, and threads share the address space.
Speed: threads are generated quickly, communication between threads is fast, and switching is fast because they are in the same address space;
resource utilization: the resource utilization of threads is good because they are in the same address space;
synchronization problems: when threads use common variables/memory A synchronization mechanism is required because it is in the same address space;

4. Mutex mutex is the mutual exclusion between processes; and critical section is the mutual exclusion of threads.

5. The way the process enters the waiting state: the CPU schedules a thread with a higher priority, and the original thread enters the waiting; the blocked thread obtains a resource or signal and enters the waiting; the time slice rotates, and the time slice arrives and enters the wait.

6. The advantages and principles of garbage collection:
Java programmers do not need to consider memory management when writing programs. With the garbage collection mechanism, java objects no longer have the concept of scope, only the reference of the object has the concept of scope.
The garbage collection mechanism can effectively prevent memory leaks and effectively use the available memory.
The garbage collector usually runs as a separate low-level thread. Clear and reclaim objects in the memory heap that have died or have not been used for a long time in unpredictable circumstances.
Programmers cannot call the garbage collector in real-time to process memory reclamation. The collection mechanism includes generational replication garbage collection, marked garbage collection, and incremental garbage collection.


Quote: Memory allocation methods in mainstream languages ​​or environments: static allocation (static variables and global variables), automatic allocation (local variables in the stack), dynamic allocation

7. What are the CPU cache and the operating system cache?
In the OS, it is a fast table: In the OS, in order to improve the access speed of the system, a small-capacity associative register is added to the address mapping mechanism, that is, the fast table, which is used to store the page numbers of the few most frequently accessed active pages. .
When the user needs to access data, the corresponding memory block number is found in the fast table according to the logical page number where the data is located, and the physical address is formed by contacting the address in the page.
If there is no corresponding logical page number in the fast table, the address mapping is performed through the memory page table to obtain the free block number, and the fast is placed in the free block of the fast table. If there are no free blocks in several fast tables, the elimination algorithm is used to eliminate them.
Fill in the new page number and block number on one line .

The high-speed buffer Cache is used in the CPU and is a temporary storage between the CPU and the arithmetic unit. The capacity is faster to access than the memory daring. The data in the Cache is a part of the memory, but this small part
will be accessed by the CPU in a period of time. When the CPU calls a large amount of data, it can avoid the memory and get it directly from the Cache, thereby speeding up the reading speed.

8. The difference between DOS and Win NT permissions:
DOS is a single-user single-task OS, when you open a computer with a DOS system, you have administrator permissions for the OS. But the configuration of permissions is not supported.
In NT, users are divided into many groups, and the groups have different permissions. A group can also have different permissions between users and users. Common user groups are: administrator group, advanced user group, common user group, guest group, and all user groups.


-------------------------------------------------- ----------
1. Convert a positive number to a string: Divide the integer by 10 at a time, each is +'0', and then reverse the order or
string the itoa function in C++ to a positive number: subtract '0', multiply 10 cumulative

2. The difference between string copy (strcpy) and memory copy (memcpy): The
strcpy function can only copy strings. Copies each character in the source string to the target string, when null or \0 is encountered, delete the character and end the copy.
The memcpy function can copy any type of data, because not all data is null-terminated, so you need to specify the number of characters for memcpy copy.
The former is usually used when copying strings, and the latter is usually used when copying other types.

3 character array does not require the last character to be \0, but character array initialization requires the last character to be \0, C[5]={'c','h','i','n','a' } definition is wrong


4. The assignment range of the char type is 0-255. If char a=256, the actual a=0

5. How to prove that a table is a circular linked list: You can set up two cursors of unequal length, such as one is 1 and the other is 2, and the two cursors start at the same time if they can meet again, the
circular linked list will be proved.

6. Global variables are placed in the data segment, and data in the data segment is statically allocated; uninitialized global variables are stored in the bss segment. BSS is the abbreviation of Block Started by Symbol in English. BSS segment belongs to static memory allocation.

The pointer is placed on the stack, pointing to the data segment of the space location;

C language has global variables, local variables, static variables, register variables. The distribution of global variables and static variables is continuous, local variables are placed on the stack,

7. The difference between
stacks: The space of the stack is automatically allocated and released by the OS, and the space on the heap is manually allocated and released.
The space of the stack is limited, and the heap is a large free storage area.

8. Direct insertion sort is the best, quick sort is the fastest. For small hours, direct insertion is best. When it is basically in order, it is best to insert the bubbling quick row directly.
When larger, merge heap sort works best.

9. The difference between
Map and HashMap: The data storage method in Map is HashTable. The main difference between Map and HashMap is the difference between interface and class. Map is an interface, and HashMap is an implementation class.
HashMap is an improved version of HashTable. It is mainly accessed by key value. object.


------------------------------------------------------------

1. Decimal=Octal/10*8+Octal%10

2. Conversion of four operators in C++: number-system conversion, for performing down conversion and conversion between inheritance, removing const, for performing unsafe implementation_dependent conversion

3. -1 in unsigned int is 65535.

A virtual function can be overridden by the subclass's virtual function of the same name. In C++, a class is instantiated: an abstract class or a constructor is private

The basic concepts of object orientation are: objects, classes, inheritance

Default member functions in C++ empty classes: default constructor, destructor, copy constructor, assignment function

Structures can have constructors and destructors, and can have inheritance. Static member variables must have initial values.

The main function is not allowed to return void in C++

Constants in the class need to be set to static or initialized in the initialization list of the constructor.

4. The destructor can be an inline function or a virtual one.

A constructor cannot be virtual, because a virtual function is a method of virtual calling. A virtual call is a mechanism that works with only partial information, allowing us to just call a
function that only knows the interface but not its exact object type. But if you want to create an object, you must know the type of the object, so the constructor cannot be a virtual function.


5. Not every function can be a virtual function: virtual functions have a cost, and each virtual function object must maintain a V table, so using virtual functions will bring certain overhead. If it's just a small class,
and you don't want to derive other classes, there's no need to use virtual functions at all.

6. Polymorphism: Simply put, it is an interface with multiple methods. The function to be called is determined during the running process of the program. Polymorphism is implemented in C++ through virtual functions.
Polymorphism allows the technique of setting a parent object equal to one or more of its child objects, and once assigned, the parent object can behave differently depending on the characteristics of the child object currently assigned to it

A virtual function is a member function that is allowed to be redefined by its subclasses. The way a subclass redefines the virtual function of the superclass is called overriding or overriding. override

Overloading means that multiple functions with the same name exist at the same time, but the number or types of parameters are different. Overloading has nothing to do with polymorphism. Has nothing to do with object orientation. overload

Encapsulation can hide implementation details and make code modular; inheritance can extend existing code blocks, both of which are for code reuse;
polymorphism is for interface reuse. Interfaces are a company's most valuable resource

------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326861053&siteId=291194637