未定义的行为可以擦除硬盘驱动器吗?
来自Scott Meyers 撰写的《Effective C++ 3rd edition》":
<块引用>为了强调未定义行为的结果是不可预测的,并且可能会非常不愉快,有经验的 C++ 程序员经常说具有未定义行为的程序会擦除您的硬盘驱动器.
在什么情况下会发生这种情况?
例如,访问和写入数组范围之外的位置是否会损坏不属于此 C++ 程序或线程的内存?
解决方案可以吗?当然.事实上发生在我身上.
我编写了删除临时目录的代码.这涉及创建一个 recursive delete
命令.由于一个错误,
字段并不总是被填充.我们的文件系统代码愉快地执行了 recursive delete *.*
命令.>
我的同事注意到桌面上的图标突然消失了.拿出两台机器.
From "Effective C++ 3rd edition by Scott Meyers":
To emphasize that the results of undefined behavior are not predictable and may be very unpleasant, experienced C++ programmers often say that programs with undefined behavior can erase your hard drive.
Under what circumstances can this happen?
For example, can accessing and writing to locations out of array range corrupt memory that doesn't belong to this C++ program or thread?
解决方案Can it? Sure. Happened to me, in fact.
I wrote code to delete a temporary directory. That involved creating a recursive delete <temp directory>*.*
command. Due to a bug, the <temp directory>
field wasn't always filled in. Our file system code happily executed the recursive delete *.*
command.
My colleagues noticed when the icons on their desktop suddenly disappeared. Took out two machines.
相关文章