The previous c++ when my friend gave me remedial knowledge points

The record

Byte alignment knowledge

A standard datatype should have its address as long as it is an integer multiple of its length, while a nonstandard datatype should be aligned according to the following rules: array: The first datatype should be aligned according to the basic datatype. Union: Align by the data type that contains the largest length.

Struct: When the data type is struct, the compiler may need to insert gaps in the allocation of struct fields to ensure that each struct element meets its alignment requirements. The starting address of the first data variable is the starting address of the data structure. Structure member variables to align emissions (aligned to the members need to fill in front of the some bytes, to ensure its alignment position), the structure itself also should according to their own effective alignment value rounded (total length is structure need to be effective alignment value structure integer times), may need to fill some space at the end of the structure at this time, To satisfy the alignment of the structure as a whole – to the largest element of the structure element. Eg: sizeof(struct Node{int a; char b; }) = 8

Union is different from structure

In the data structure union, there are many different member variables that share the same memory. For example, union u{char a[4]; int i; }u1; If I is changed,a[] will also change, and byte alignment will be based on the data type containing the largest length.

Each member of the structure has its own memory, and their use does not interfere with each other. There may be gaps in the aligned discharge of member variables.

Hash conflict

1. Open address method

(1) Linear detection

If the hash value of the data already exists, one unit is added to the original hash value until no hash conflict occurs.

(2) Re-square detection

If the hash value of the data already exists, it is added to the original hash valueOne unit kangkang, no, try againOne unit? Not yet..

(3) Pseudo-random detection

If some data already exists, add a random number to the original hash value until no hash conflicts occur.

2. Chained address method (HashMap conflict resolution method)

For the same hash value, join using a linked list. Use arrays to store each list.

Advantages:

(1) The processing is simple and the average search length is short;

(2) The node space of each linked list is applied dynamically, which is suitable for the situation that the table length cannot be determined;

(3) In order to reduce conflicts, the open addressing method requires a small loading factor α, so a lot of space will be wasted when the node scale is large. In the zipper method, α≥1 is desirable, and when the node is large, the pointer field added in the zipper method can be ignored, so space is saved.

(4) Convenient deletion of nodes disadvantages:

When Pointers occupy a large space, space will be wasted. If space is used to increase the size of hash table, then the efficiency of open address method will be improved.

3. Establish a public overflow area

4. Hash again hash the conflicting hash values until there is no hash conflict.

Vector memory implementation

One of the characteristics of a vector is that its memory grows, not decreases. To support fast, random access, the elements of a vector are stored consecutively, with each element next to the one before it.

As a result, the STL implementation allocates more memory to a vector than it currently needs.

Each time push_back is called, the underlying array implementation is resized. This is reflected in the vector implementation, where each push_back element is reallocated, the original element is copied to the new store, the original element is copied to the new store, and the original vector is destructed and freed.

The static keyword

Static variables: Thread unsafe.

Static variables can only be placed in classes, not methods.

Static variables have default initialization values. Global variables and static variables are not initialized in the BSS area, after initialization in the initialization area.

Static variables represent an attribute shared by all instances, located in the method area, sharing a share of memory, and member variables are a special description of the object, the instance variables of different objects are allocated in different memory space, once the static variable is modified, other objects are visible to the modification, so the thread is not safe.

Static local variables are initialized only once, and the last value is recorded (= recorded value)

Because multithreads are executed concurrently and the system schedules randomly, various locking mechanisms are used to ensure thread safety and correct execution.

Static functions: Static member functions cannot call non-static member functions, but the reverse is possible,

Static functions are restricted to use in this source file and cannot be called outside this source file. Can’t use this

Linux read-write lock

A read/write lock is a lock that can be in write state and read state. In a read-mode lock, all threads attempting to acquire it in read-mode can acquire it, and all threads attempting to acquire it in write mode are blocked. In write mode, both read and write locks are blocked. Read/write locks are shared mutex locks.

Simply put, in read mode, all processes can acquire read locks, but no process can acquire write locks.

In write mode, the read/write lock becomes a mutex, and only one thread can acquire the lock.

Extern write C ++ in C language, link C ++ function library, link

C language indefinite parameter

(1)

#include<stdarg.h>

type va_arg(va_list, type);

void va_start(va_list ap, last);

void va_end(va_list ap);

Use the sample

int test(int count, short first, ...) { va_list argp; int sum = 0; int tmp = 0; va_start(argp, first); printf("%d \t", first); for (int i = 1; i < count; ++i) { printf("%d \t", va_arg(argp,short)); } va_end(argp); return 0; }Copy the code

(2)

Variable parameter macros… And new macros in the __VA_ARGS__ C99 specification

#define debug(format, …) fprintf(stdout, format, __VA_ARGS__)

int test(int count, short first, …)

{

va_list argp;

int sum = 0;

int tmp = 0;

va_start(argp, first);

printf(“%d \t”, first);

for (int i = 1; i < count; ++i)

{

debug(“%d \t”, va_arg(argp,short));

}

va_end(argp);

return 0;

}

Compilation process

1. Preprocessing – macro expansion, header file

2. Compile into assembly file lexical analysis grammar analysis

3. The compiler executes assembly files to generate machine language code

4. The invoked symbol/library function is linked in

Static link library and dynamic link library programming

A. so b. so C. so D. so

Static linked libraries: When used, the linker finds the functions needed by the program and copies them to the executable file. Since this copy is complete, once the connection is made, the static libraries are no longer needed.

For dynamically linked libraries: when a program calls a dynamically linked library function during execution, the operating system first looks at all running programs to see if there is a copy of the library function in memory. If so, let it share that copy. Link loads only if there is none. While the program is running, the called dynamically linked library function is placed somewhere in memory, and all programs calling it will point to this code segment. Therefore, the code must use relative addresses, not absolute addresses. At compile time, we need to tell the compiler that these object files are used for dynamically linked libraries, so Position Independent Code (PIC) is used.

Three handshakes and four waves

Three handshakes:

  1. First handshake: Establish a connection. The client sends a connection request packet segment and sets the SYN position to 1 and Sequence Number to X. Then, the client enters the SYN_SEND state and waits for confirmation from the server.

  2. Second handshake: The server receives a SYN packet segment. Context The server should acknowledge the SYN segment received from the client, and set this Acknowledgment Number to X +1(Sequence Number+1). Set the SYN position to 1 and Sequence Number to y. The server puts all the above information into a packet segment (namely, SYN+ACK packet segment) and sends it to the client. At this time, the server enters the SYN_RECV state.

  3. Third handshake: The client receives a SYN+ACK packet from the server. A. Then set this Acknowledgment Number to Y +1 and send an ACK segment to the server. After this segment is sent, both the client and the server enter the ESTABLISHED state and complete the TCP three-way handshake.

Four waves:

  1. First wave: Host 1 (either client or server), set the Sequence Number and Acknowledgment Number, and send a FIN segment to host 2. At this point, host 1 enters the FIN_WAIT_1 state. This means that host 1 has no data to send to host 2.

  2. Second wave: Host 2 receives the FIN segment from host 1 and sends an ACK segment back to Host 1. This Acknowledgment Number is set to Sequence Number plus 1. Host 1 enters the FIN_WAIT_2 state. Host 2 tells host 1 that I “agree” to your shutdown request;

  3. Third wave: Host 2 sends a FIN packet to host 1 to close the connection, and host 2 enters the LAST_ACK state.

  4. Fourth wave: Host 1 receives the FIN packet from host 2 and sends an ACK packet to host 2. Then host 1 enters the TIME_WAIT state. Host 2 closes the connection after receiving the ACK packet from host 1. If host 1 does not receive a reply after waiting for 2MSL, then the Server is shut down.

Why does TCP have three handshakes instead of two?

To ensure reliable data transmission, both parties of the TCP protocol must maintain a serial number to identify which packets have been received. The three-way handshake is a necessary step for communication parties to inform each other of the start sequence number and confirm that the other party has received the start sequence number. In the case of two handshakes, at most only the initial sequence number of the connection initiator can be confirmed, and the sequence number selected by the other party cannot be confirmed.

Why is there a three-way handshake when you connect and a four-way handshake when you close?

After receiving a SYN request packet from the Client, the Server sends a SYN+ACK packet. ACK packets are used for reply, and SYN packets are used for synchronization. However, when the Server receives a FIN packet, the SOCKET may not be closed immediately. Therefore, the Server can only reply with an ACK packet to tell the Client, “I received the FIN packet you sent.” I can send FIN packets only after all packets on the Server are sent. Therefore, THE FIN packets cannot be sent together. Therefore, a four-step handshake is required.

Reasons for the existence of TIME_WAIT:

(1) Reliably realize the termination of TCP full-duplex connection

If the final ACK is lost, the server resends the FIN, so the client must maintain status information to avoid receiving an ACK after entering another state and then reporting an error.

(2) Let the old lost section disappear in the network

Zombie progression and orphan progression generation and avoidance

Constant Pointers and pointer constants

Extern “C” function

C++ code calls C code — Because C++ supports function overloading and C does not support it, C++ compacts functions with symbols that are different from C, such as parameters, etc. If C++ is not extern “C” restrictive, C++ links may not find paths and result in an error.

The role of LLD

In Linux, LDD stands for List, dynamic, and Dependencies, which lists dynamic library dependencies.

What GCC options are used to generate preprocessed files and what GCC options are used to generate assembly

www.itcodemonkey.com/article/153…