惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
WordPress大学
WordPress大学
T
The Exploit Database - CXSecurity.com
博客园_首页
MyScale Blog
MyScale Blog
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
F
Full Disclosure
V
V2EX
博客园 - Franky
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
SecWiki News
SecWiki News
N
Netflix TechBlog - Medium
S
Secure Thoughts
酷 壳 – CoolShell
酷 壳 – CoolShell
Hacker News: Ask HN
Hacker News: Ask HN
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Martin Fowler
Martin Fowler
PCI Perspectives
PCI Perspectives
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
Help Net Security
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
AI
AI
Microsoft Azure Blog
Microsoft Azure Blog
K
Kaspersky official blog
G
GRAHAM CLULEY
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
CERT Recently Published Vulnerability Notes
U
Unit 42
T
Tor Project blog
Cloudbric
Cloudbric
Hacker News - Newest:
Hacker News - Newest: "LLM"
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

kekxv 技术日志

基于 kekxv/gitea-pages 与 Gitea Actions 构建静态站点托管服务 Json简单工具 在Windows上运行Code Server:结合WSL打造你的云端VS Code开发环境 安卓sdkmanager工具换源 boost bazel starter bazel 供应商模式 PVE引导丢失修复 NSFW图像检测 关于内网springboot启动慢记录 网页转换为chrome插件 nginx代理的一种使用方式 YOLOv8 训练自己的数据 luckfox-交叉编译之bazel gitea actions CICD 自动化 Linux限制进程使用率 影音中心Jellyfin快速部署 OCR & 人脸算法 -- opencv dnn tensorflow gpu 安装(ubuntu22.04) 深度学习记录-简单
警惕c++内置变量指针
kekxv · 2024-06-11 · via kekxv 技术日志

今天发现一个计算时间的BUG,每次执行操作的时候,结果居然不一样。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
std::string GetTime(const std::time_t &currentTime, int offset, const char *format_string) {
char buffer[80];
const auto *tm = std::localtime(&currentTime);
std::strftime(buffer, sizeof(buffer), format_string, tm);
return buffer;
}
int main(){
time_t time_offset = time(nullptr);
struct tm *now_ = localtime(&t);
std::cout << GetTime(time_offset - now.tm_wday * 24 * 60 * 60, 0,"%Y-%m-%d") << std::endl;
std::cout << GetTime(time_offset - now.tm_wday * 24 * 60 * 60, 0,"%Y-%m-%d") << std::endl;
return 0;
}

两次打印出来的时间居然不是一致的!

原因是:std::localtime返回的指针是内部变量,下次再调用std::localtime之后,将会对它进行更新,导致每次now.tm_wday实际的值已经改变了。

解决的方法也很简单,对std::localtime进行拷贝就好了。

以后再看到直接返回指针的函数,都要留一下心,看看是不是内部的变量的指针!