0%

内存分配

std::make_shared 执行一次堆分配,而调用std::shared_ptr 构造函数执行两次

在一个典型的实现中 std::shared_ptr 管理两个实体:

  • 控制块(存储元数据,如引用计数、类型擦除删除器等)
  • 被管理的对象

控制块是一个动态分配的对象,它包含:

  • 指向托管对象的指针或托管对象本身;
  • 删除器 (类型擦除)
  • 分配器 (类型擦除)
  • 拥有被管理对象的 shared_ptr的数量
  • 引用托管对象的 weak_ptr 的数量

std::make_shared执行一次堆分配,计算控制块和数据所需的总空间。在另一种情况 std::shared_ptr<Obj>(new Obj("foo"))下执行两次, new Obj("foo")为托管数据调用堆分配,std::shared_ptr构造函数为控制块执行另一个堆分配。

阅读全文 »

.dSYM

.dSYM (debugging SYMbols) 又称为调试符号表,是苹果为了方便调试和定位问题而使用的一种调试方案,本质上使用的是起源于贝尔实验室的 DWARFDebugging With Attributed Record Formats),其在.xcarchive目录中的层次结构为:

1
2
3
4
5
6
.xcarchive
--dSYMs
|--Your.app.dSYM
|--Contents
|--Resources
|--DWARF
阅读全文 »

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
➜ sudo apt update 
命中:1 https://pro-driver-packages.uniontech.com eagle InRelease
获取:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute InRelease [269 kB]
命中:3 http://packages.microsoft.com/repos/code stable InRelease
命中:4 https://home-packages.chinauos.com/home plum InRelease
命中:5 https://home-packages.chinauos.com/home plum/beta InRelease
命中:6 https://home-packages.chinauos.com/printer eagle InRelease
错误:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute InRelease
由于没有公钥,无法验证下列签名: NO_PUBKEY 871920D1991BC93C
命中:7 https://home-store-img.uniontech.com/appstore eagle InRelease
正在读取软件包列表... 完成
W: GPG 错误:http://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY 871920D1991BC93C
E: 仓库 “http://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute InRelease” 没有数字签名。
N: 无法安全地用该源进行更新,所以默认禁用该源。
N: 参见 apt-secure(8) 手册以了解仓库创建和用户配置方面的细节。

执行:

1
➜ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 871920D1991BC93C

Item 26:Postpone variable definitions as long as possible.

推迟变量的定义有两个好处:

  • 改善程序效率,减少无用的构造和析构。
  • 增加程序流程清晰度。

这条规则看似简单,但存在流程控制语句的时候容易疏忽。如:

1
2
3
4
5
6
7
8
9
string encryptPassword(const string& password){
string encrypted;
if (password.length() < MinimumPasswordLength) {
throw logic_error("Password is too short");
}
encrypted = password;
encrypt(encrypted);
return encrypted;
}
阅读全文 »

Item 25: Consider support for a non-throwing swap.

swap 函数能置换两对象值,功能很重要!

std 的缺省基本实现如下:

1
2
3
4
5
6
7
8
namespace std {
template <typename T>
void swap(T& a, T& b) {
T temp(a);
a = b;
b = temp;
}
}
阅读全文 »