Search code examples
c++memoryundefined-behavior

Can undefined behavior erase the hard drive?


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?


Solution

  • 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.