C++ 中文周刊 第91期
C++ 中文周刊 第91期
资讯
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2022-11-30 第178期
文章
离谱,freebsd的ping 有溢出漏洞
关键字 | 放在变量前 | 放在static/thread_local前 | 放在函数前 | 常量表达式前 |
---|---|---|---|---|
const | 可以 | 可以 | 成员函数 | 有时可以 |
constexpr | 可以 (初始化得是constexpr的) | 可以 | 必须是满足constexpr属性的函数 | 可以 |
consteval | 不行 | 不行 | 满足consteval的函数 | 接受返回值,可以 |
constinit | 不行 | 必须常量初始化 | 不行 | 不行 |
未定义行为
不一定是实现决定,未定义行为
就是 未定义行为
, 就是可能任何事都会发生,别想当然
列了一堆c的资料以及语法细节
[[nodiscard]] constexpr auto if_hell(bool c1, bool c2) {
if (c1) {
if (c2) {
return true;
} else {
throw;
}
}
// ...
return false;
}
static_assert(not if_hell(false, false));
static_assert(not if_hell(false, true));
assert(throws([]{ if_hell(true, false)); }));
static_assert(if_hell(true, true));
[[nodiscard]] constexpr auto if_heaven(bool c1, bool c2) {
if (not c1) {
return false;
}
if (not c2) {
throw;
}
// ...
return true;
}
static_assert(not if_heaven(false, false));
static_assert(not if_heaven(false, true));
assert(throws([]{ if_heaven(true, false)); }));
static_assert(if_heaven(true, true));
别写if_hell这种面条。尽量提前结束
看代码
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
uint8_t tab[0x1ff + 1];
uint8_t f(int32_t x)
{
if (x < 0)
return 0;
int32_t i = x * 0x1ff / 0xffff;
if (i >= 0 && i < sizeof(tab)) {
printf("tab[%d] looks safe because %d is between [0;%d[\n", i, i, (int)sizeof(tab));
return tab[i];
}
return 0;
}
int main(int ac, char **av)
{
return f(atoi(av[1]));
}
一个经典UB,既然溢出是UB,那么GCC就假定永远不会溢出,这里的条件判断统统删除,所以执行一个溢出的数,直接coredump
大家有时间可以读一下UB指南 https://blog.regehr.org/archives/213 没工夫读的,我总结一下
说过挺多次了。就是有些场景引用可能带来副作用,对于引用主动decay copy成值语义
讲反射的。代码写的和boost.fpr(magic_get)
差不多吧
用静态分析抓bug。场景都特简单
int main() {
std::vector<int> data{ 1,1,2,3,5,8,13,21,34,55 };
for (auto it = data.begin(); it != data.end();) {
/* do something with *it */
if (*it % 2 == 0) {
data.erase(it);
} else {
++it;
}
}
for (auto const& e : data)
std::cout << e << '\n';
}
问题出在哪里?入门级
代码写错,
if (memcmp(m_result_original, m_result_my_version, sizeof(struct tmp))!=0)
if (memcmp(m_result_original, m_result_my_version, sizeof(struct tmp)!=0)) //括号匹配错了
第二种写法gcc告警很不清晰
2.c:17:72: warning: size argument in 'memcmp' call is a comparison [-Wmemsize-comparison]
if (memcmp(m_result_original, m_result_my_version, sizeof(struct tmp)!=0))
~~~~~~~~~~~~~~~~~~^~~
2.c:17:7: note: did you mean to compare the result of 'memcmp' instead?
if (memcmp(m_result_original, m_result_my_version, sizeof(struct tmp)!=0))
^ ~
)
2.c:17:54: note: explicitly cast the argument to size_t to silence this warning
if (memcmp(m_result_original, m_result_my_version, sizeof(struct tmp)!=0))
^
(size_t)( )
1 warning generated.
这种问题怎么规避?
介绍oilpan的。一个GC框架,Chrome V8引入这个GC,又支持了pointer compression 指针压缩。指针48位明显用不完,olipan搞到了32位,还有压缩空间
- What kind of caller diagnostic information can I get from exceptions thrown by C++/WinRT and wil?
- What kind of caller diagnostic information can I get from exceptions thrown by C++/WinRT and wil?, C++20 edition
- On Windows Runtime asynchronous operations with critical progress reports
- The case of the application that used thread local storage it never allocated
- Not even trying to cross an airtight hatchway: Calling a function in your own process by synthesizing a function pointer
讲windows API的。没怎么看懂。Raymond Chen高产似母猪啊
看不懂
视频
代码在这里 https://github.com/stdgraph/graph-v2
老哥非常乐观
还能这么玩???
开源项目需要人手
- asteria 一个脚本语言,可嵌入,长期找人,希望胖友们帮帮忙,也可以加群384042845和作者对线
- pika 一个nosql 存储, redis over rocksdb,非常需要人贡献代码胖友们, 感兴趣的欢迎加群294254078前来对线
新项目介绍/版本更新
- tag ptr
- mini-rv32ima 模拟器,不懂
工作招聘
开始刷leetcode了。现在工作真卷啊,算法题我都忘光了
看到这里或许你有建议或者疑问或者指出错误,请留言评论! 多谢! 你的评论非常重要!
相关文章
- Jgit的使用笔记
- 利用Github Action实现Tornadofx/JavaFx打包
- 叹息!GitHub Trending 即将成为历史!
- 微软软了?开源社区讨论炸锅,GitHub CEO 亲自来答
- GitHub Trending 列表频现重复项,前后端都没去重?
- Photoshop Elements 2021版本软件安装教程(mac+windows全版本都有)
- (ps全版本)Photoshop 2020的安装与破解教程(mac+windows全版本都有)
- (ps全版本)Photoshop cc2018的安装与破解教程(mac+windows全版本,包括2023
- 环境搭建:Oracle GoldenGate 大数据迁移到 Redshift/Flat file/Flume/Kafka测试流程
- 每个开发人员都要掌握的:最小 Linux 基础课
- 来撸羊毛了!Windows 环境下 Hexo 博客搭建,并部署到 GitHub Pages
- 超实用!手把手入门 MongoDB:这些坑点请一定远离
- 【GitHub日报】22-10-09 zustand、neovim、webtorrent、express 等4款App今日上新
- 【GitHub日报】22-10-10 brew、minio、vite、seaweedfs、dbeaver 等8款App今日上新
- 【GitHub日报】22-10-11 cobra、grafana、vue、ToolJet、redwood 等13款App今日上新
- Photoshop 2018 下载及安装教程(mac+windows全版本都有,包括最新的2023)
- Photoshop 2017 下载及安装教程(mac+windows全版本都有,包括最新的2023)
- Photoshop 2020 下载及安装教程(mac+windows全版本都有,包括最新的2023)
- Photoshop 2023 资源免费下载(mac+windows全版本都有,包括最新的2023)
- 最新版本Photoshop CC2018软件安装教程(mac+windows全版本都有,包括2023