mmap主要用来做内存映射的,可以将虚拟内存和磁盘上的文件直接映射。共享映射中有一种机制叫做sync同步机制,对一端的修改会实时同步到另一端,这也是通过文件共享映射实现进程通信的基础。
9.1 什么是零拷贝? | 小林coding (xiaolincoding.com)
c++之指针和引用作为函数参数传递时的区别_指针和引用作为函数参数区别-CSDN博客
四种类型转换
static_cast
是一种静态的转换,在编译期就能确定的转换
dynamic_cast
不太安全(非法访问)
常量指针被转化成非常量的指针,并且仍然指向原来的对象
[C++11] 理解右值引用及相关概念(左值与右值、std::move、完美转发)_std::move和完美转发-CSDN博客
牛逼:
#include <iostream>
#include <utility> // For std::forward
// 模拟一个可以接收左值引用和右值引用的函数
void process(int& x) {
std::cout << "process(int&) - Lvalue reference" << std::endl;
}
void process(int&& x) {
std::cout << "process(int&&) - Rvalue reference" << std::endl;
}
// 模板函数,使用完美转发将参数转发给process函数
template<typename T>
void wrapper(T&& arg) {
// 使用std::forward来转发参数,保持其原始的类型和值类别
process(std::forward<T>(arg));
}
int main() {
int a = 10;
wrapper(a); // 编译器将调用 process(int&),因为a是左值
wrapper(10); // 编译器将调用 process(int&&),因为10是临时对象(右值)
}
失败案例:
template<typename T>
void forward_to_function(T param) {
// 没有使用 std::forward,直接传递参数
some_other_function(param);
}
void some_other_function(int& lval) {
// 接受左值引用
std::cout << "Lvalue reference to int" << std::endl;
}
void some_other_function(int&& rval) {
// 接受右值引用
std::cout << "Rvalue reference to int" << std::endl;
}
int main() {
int x = 10;
// 传递左值
forward_to_function(x); // 预期输出: Lvalue reference to int
// 传递右值
forward_to_function(20); // 预期输出: Rvalue reference to int
return 0;
}