# 视频
[https://www.bilibili.com/video/BV17HWnzAEWx/?share_source=copy_web&vd_source=c2640429ffb126a929d74212d5168b1f](https://www.bilibili.com/video/BV17HWnzAEWx/?share_source=copy_web&vd_source=c2640429ffb126a929d74212d5168b1f)
# 注意
文稿与视频实际内容可能存在出入,以视频为准
# 关于评论区的一些评论
## 很少能看到这么正确的[科普视频](https://search.bilibili.com/all?from_source=webcommentline_search&keyword=%E7%A7%91%E6%99%AE%E8%A7%86%E9%A2%91&seid=10340578035520107415&from_avid=115390118234720&from_comid=279534336928),居然用的是[devcpp](https://search.bilibili.com/all?from_source=webcommentline_search&keyword=devcpp&seid=10340578035520107415&from_avid=115390118234720&from_comid=279534336928)
本来是想用VSCode的,但是我当时忘记了就用Devc++
## 都是基本功,还算可以。不过工程上极度不推荐用这个万能头文件和[using namespace](https://search.bilibili.com/all?from_source=webcommentline_search&keyword=using%20namespace&seid=10340578035520107415&from_avid=115390118234720&from_comid=277979618801) std 虽然大概率你是打[算法竞赛](https://search.bilibili.com/all?from_source=webcommentline_search&keyword=%E7%AE%97%E6%B3%95%E7%AB%9E%E8%B5%9B&seid=10340578035520107415&from_avid=115390118234720&from_comid=277979618801)的,不太可能会重视这种东西。
是OI
# 文稿
## 1. 变量初始化之谜
cpp
```
#include
using namespace std;
int main() {
int x;
cout << "未初始化的x = " << x << endl;
return 0;
}
```
**功能**:演示未初始化局部变量的行为
**你以为是确定性的0吗?**
**实际上是随机垃圾值的!**
**原理**:局部变量在栈上分配,不会自动初始化,值是之前使用该内存的残留数据
## 2. 浮点数精度陷阱
cpp
```
#include
using namespace std;
int main() {
double a = 0.1;
double b = 0.2;
double c = 0.3;
cout << "0.1 + 0.2 == 0.3 ? " << (a + b == c) << endl;
cout << "实际值: " << a + b << endl;
return 0;
}
```
**功能**:验证浮点数精度问题
**你以为是精确相等的吗?**
**实际上是不相等的!**
**原理**:浮点数在二进制中无法精确表示0.1、0.2这样的十进制小数,存在精度误差
## 3. 数组越界不报错
cpp
```
#include
using namespace std;
int main() {
int arr[3] = {1, 2, 3};
cout << "arr[5] = " << arr[5] << endl;
cout << "程序还在运行!" << endl;
return 0;
}
```
**功能**:测试数组越界访问的行为
**你以为是会崩溃的吗?**
**实际上是可能正常输出的!**
**原理**:C++不检查数组边界,越界访问是未定义行为,可能读到随机内存值而不崩溃
## 4. 布尔值的算术运算
cpp
```
#include
using namespace std;
int main() {
bool b = true;
cout << "b + 5 = " << (b + 5) << endl;
cout << "b * 10 = " << (b * 10) << endl;
return 0;
}
```
**功能**:展示bool类型在算术运算中的行为
**你以为是true/false的吗?**
**实际上是1/0参与运算的!**
**原理**:bool在算术运算中会隐式转换为int,true为1,false为0
## 5. 自增运算符的迷惑行为
cpp
```
#include
using namespace std;
int main() {
int i = 5;
int j = i++ + ++i;
cout << "j = " << j << endl;
return 0;
}
```
**功能**:演示序列点问题
**你以为是确定结果的吗?**
**实际上是编译器随缘的!**
**原理**:同一表达式中对同一变量多次修改是未定义行为,结果取决于编译器实现
## 6. 空结构体的大小
cpp
```
#include
using namespace std;
struct Empty {};
int main() {
cout << "空结构体大小: " << sizeof(Empty) << endl;
return 0;
}
```
**功能**:探究空结构体的内存占用
**你以为是0字节的吗?**
**实际上是1字节的!**
**原理**:为了确保每个对象都有唯一地址,空结构体被分配1字节大小
## 7. const变量的"不可修改"
cpp
```
#include
using namespace std;
int main() {
const int x = 10;
int* p = const_cast(&x);
*p = 20;
cout << "x = " << x << endl;
cout << "*p = " << *p << endl;
return 0;
}
```
**功能**:测试const_cast的能力和限制
**你以为是真正不可变的吗?**
**实际上是可以绕过的!**
**原理**:const_cast可以移除const属性,但修改const对象是未定义行为
最后修改:2026 年 04 月 28 日
©
此处评论已关闭