当前位置:网站首页>450 Shenxin Mianjing 1
450 Shenxin Mianjing 1
2022-07-02 19:34:00 【liufeng2023】
1、 When are copy constructors and assignment functions used , What should I pay attention to ?
- copy constructor : Initialize a with an existing object New creation When an object is called .
- Assignment constructors : Use an existing object against another There is already an object When assigning values, call .
Be careful :
- When a pointer in a class points to heap space , These two constructors need to be noted that “ Shallow copy ” The problem of ( Two pointers point to the same heap space , The space is released twice ).
2、extern "c" What's the difference between adding and not adding ?
function and Variable front Add extern Keywords difference :
- extern External keywords ,extern You can modify variables or functions ( Function is defaulted by extern modification );
- Indicates that the definition of this variable or function is in other files , Prompt the compiler to find its definition in other files when encountering this variable and function .
Dynamic link library DLL Library plus extern “C” The role of :
- stay C/C++ Mixed programming is used , stay DLL Add extern “C” Indicates that this function uses C Language .
3、 Memory alignment principles
What is memory alignment ?
- In many CPU Under the architecture ,CPU Instructions require that the starting address of the operation memory can be divided by the size of the operation memory ; Memory access that meets this requirement is called access aligned memory ;
- Generally speaking, the compiler will automatically align the memory .
- Memory alignment is the arrangement rule of data storage in memory , Easy to read by hardware .
Memory alignment factor :
#pragma pack(n) To set variables to n Byte alignment .
Memory alignment rules :
1、 The first data member of the structure is placed at the offset offset by 0 The place of , In the future, the alignment of each data member is in accordance with #pragma pack The specified value and The length of the data member itself in , Than Align smaller ( Offset to first address , The specified value needs to be a multiple of this small number ).
2、 After the data members are aligned , class ( Structure or union ) Alignment itself , The alignment will follow #pragma pack The specified value and structure ( Or in association with ) Maximum data member length in ( The smaller one is aligned , The specified value needs to be a multiple of this small number .)
3、 Byte alignment is Determined at compile time , Once the decision is made, it will not change , Never change the size during operation .
Why memory alignment ?
- The platform has good portability ;
- CPU High execution efficiency ;
example 1:

example 2:
With 32 For example, bit system ;
Code 1:
struct A{
short a;
short b;
short c;
};
struct B{
struct A a;
char c;
int d;
};
printf("%d\n",sizeof(struct B));//-- Output 12
Code 2:
struct A{
short a;
short b;
short c;
char d;
};
struct B{
struct A a;
char c;
int d;
};
printf("%d\n",sizeof(struct B));//-- Output 16
For code 1:
- struct A It was based on short 2 Byte Alignment ,struct A Itself is 6 Bytes , But put him in struct B In the middle of the day , Is due to struct B Is in accordance with the int 4 Byte alignment , So we need to struct B to struct A Separate out 2 Bytes of space is used to gather 4 Byte alignment , this 2 A byte is composed of struct B Provided , therefore struct B Members in can occupy , That is to say struct B In the char It can occupy , So the total is 12 Bytes .
For code 2:
- struct A according to short 2 Byte alignment ,struct A The size of itself is 8 byte , in other words struct A The last one char In the back 1 Bytes of space , But notice , this 1 The space of bytes is due to struct A Self aligned , It belongs to struct A Of , So will struct A Put in struct B In the middle of the day , although struct B There is one of them. char, But it can't be occupied struct A The remaining one in 1 Bytes of space , Only one more can be allocated 4 Bytes are used to store , After storage, there is 3 Bytes are not enough to store the following int 了 , redistribution 4 Bytes are used to store int, So the total is 16 Bytes .
4、memcpy Can the parameter be a structure ?
Sure !
- because mem The first ones are memory copies , You can copy any data .( Copy structure to array , Copy structure to structure , The array can be copied to the structure ).
6、 A chessboard , There is a horse , Give a starting position and an ending position , How to find the shortest path ?
backtracking ( Like a sword finger offer–13. The range of motion of the robot ), Only the number of shortest paths can be found .
7、epoll Characteristics and differences between horizontal trigger and edge trigger ?
Level trigger ( The default mode ):
- If the kernel does not respond socket The data sent corresponding to the event is read ( If the buffer is too small ), The next time epoll Will still put this socket The corresponding event is added to the response queue , Until the data in the buffer is read .
Edge trigger ( High speed mode ):
- The kernel will not respond socket The data sent by the event is read , The next time epoll I won't prompt this again socket event , Until the next time there is data written, it will respond again .
Combined with the following 8 The blocking and non blocking of can be seen :
- Whether blocking or non blocking , When reading data, you need to use while Cycle to read all the data
- (LT The mode can be omitted while But it didn't work , Because the complete data cannot be processed at one time , You need to wait for the next response before continuing to receive the rest of the data , So it's still used once while Receive the data )( Because every time recv Receiving is not guaranteed to make the set length , It may be smaller than this length ).
epoll Of ET( Edge trigger ) Mode must use non blocking IO:
Because if blocking is used IO:
- Case one , Don't use while Read all the data , Then the data read after the next event may be the content left in the buffer of the previous event , This will affect the handling of this incident .
- The second case , Use while Read out all the data of each response event , that The last time recv Will be stuck here when , Because it's clogged , Then this thread will be invalidated , You can only wait for the client to send the next data read before responding , That is, this thread can only serve this client .
Just use while To ensure that the data is read , You can't use blocking IO.
If you use it every time while Process the data together after reading it completely , There is no difference between the two modes ;
In the following cases :
- If there is a file descriptor of interest but this event is not handled , that LT In mode epoll Will always return the response of this event , and ET In mode, it will not .
8、 Blocking and non blocking fd Of recv perhaps send The difference between ?
In blocking mode :
- send: wait for send The function copies all data into the send buffer before returning .
- recv: If No data, just waiting , When there's data coming ,Recv Function copies the contents of the receive buffer to the application layer buffer in ( Be sure to pay attention to , Not all copies are returned here , Instead, as long as there is copied data, it will be returned , That is, it may be smaller than the length of data you want to accept ).
In non blocking mode :
- send: The function immediately returns , Between call and return Write data into the send buffer as much as possible . When to send is decided by the system .
- recv: If No data is returned directly , If there's data Copy the contents of the receive buffer to the application layer buffer in Just go back to !( It may not be all finished , Same as blocking ).
PS: Although blocking and non blocking Recv The working principle of receiving data is similar , But the main difference is whether to return directly when there is no data to receive .
9、udp How to quickly locate clients that are not responding ?
- Send a broadcast , The client receives a reply ;
- because UDP Packets may be lost , You can send multiple broadcasts , List the clients that do not reply once as invalid clients .
10、 How to be in 40 One hundred million , Data is out of order , Give a number , How to find this number quickly ?
problem :
- On a given set 4G Of PC Implemented on the machine , A contain 40 Hundreds of millions of unrepeated and unordered unsigned int Integers , Give an integer , Find a given number m, Is it in the file 40 Out of 100 million data .
Demand analysis :
int Type in the C++ Storage occupation in 4 individual Byte,32Bit;
If defined in memory 40 One hundred million int Type array to read files , Occupation size :
(40* 100000000*4/1024/1024/1024)G=14.901G.This is far beyond the memory limit of the machine , At this time, the conventional thinking is , Save data on disk , Read data into memory in batches , But this will Generate disks IO, For us who pursue speed , In this case, we don't consider .
For data , Is there a certain data or not , We can use 0 and 1 To express , This just matches binary 0 1 attribute ;
We know C++ in 1 int = 4 Byte = 32 Bit; In this case , The memory space we need is ,(40*100000000/8/1024/1024)M = 476.8! It actually meets our requirements .
Specific ideas :
1 individual int Occupy 4 Byte is 4*8=32 position , Then we only need to apply for one int The array length is int tmp[1+N/32] You can store all this data ( among N Represents the total number of searches to be made ,tmp Each element in is in memory 32 Bits can correspond to decimal numbers 0~31)
The following is bitmap algorithm ;( Reference resources https://blog.csdn.net/Edward_LF/article/details/124614606)
边栏推荐
- 冒泡排序数组
- 虚拟机初始化脚本, 虚拟机相互免秘钥
- Gmapping code analysis [easy to understand]
- Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode
- 搭建哨兵模式reids、redis从节点脱离哨兵集群
- 守望先锋世界观架构 ——(一款好的游戏是怎么来的)
- 全志A33使用主线U-Boot
- AcWing 181. 回转游戏 题解(搜索—IDA*搜索)
- AcWing 1127. 香甜的黄油 题解(最短路—spfa)
- AcWing 383. Sightseeing problem solution (shortest circuit)
猜你喜欢

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management

教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5

End-to-End Object Detection with Transformers(DETR)论文阅读与理解

数据降维——因子分析

Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly

xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机

Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode

全志A33使用主线U-Boot

IEDA refactor的用法
随机推荐
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
A4988 drive stepper motor "recommended collection"
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Thread application instance
Codeforces Round #802 (Div. 2) 纯补题
AcWing 1125. Cattle travel problem solution (shortest path, diameter)
《重构:改善既有代码的设计》读书笔记(上)
Emmet基础语法
线程应用实例
C file input operation
《代碼整潔之道》讀書筆記
使用xml文件打印mybaties-log插件的方式
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
Implementation of 452 strcpy, strcat, StrCmp, strstr, strchr
Advanced performance test series "24. Execute SQL script through JDBC"
Notes de lecture sur le code propre
Memory management of C
Golang:[]byte to string