未定义的行为可以擦除硬盘驱动器吗?

时间:2023-05-08
本文介绍了未定义的行为可以擦除硬盘驱动器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

来自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.

这篇关于未定义的行为可以擦除硬盘驱动器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:你能在 c 或 c++ 中分配一个非常大的单个内存块(> 4GB)吗? 下一篇:C++中对象是如何存储在内存中的?

相关文章