通过更改C ++中的内存损坏计算机的风险 [英] Risk of damaging your computer by altering memory in C++

查看:73
本文介绍了通过更改C ++中的内存损坏计算机的风险的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一些Java,现在正在尝试使用C ++,显然在C ++中,您可以执行以下操作:声明一个大小为6的int数组,然后更改该数组的第10个元素,据我所知,它只是第4个元素为6整数数组分配的内存部分的末尾之后的字节.

I know some Java and am right now trying C++ instead and apparently in C++ you can do things like declare an int array of size 6, then change the 10th element of that array, which I'm understanding to be simply the 4th byte after the end of the section of memory that was allocated for the 6-integer array.

所以我的问题是,如果我粗心大意,是否有可能偶然更改系统上其他程序正在使用的C ++程序中的内存?是否存在以这种方式严重弄乱某些东西的实际风险?我的意思是,我知道您可以根据需要重新启动计算机并清除内存,但是,如果我不这样做,可能会造成持久的损坏.

So my question is, if I'm careless is it possible to accidentally alter memory in my C++ program that is being used by other programs on my system? Is there an actual risk of seriously messing something up this way? I mean I know you can just restart your computer and clear the memory if you have to, but if I don't do that, there could be some lasting damage.

推荐答案

这取决于您的系统.形式上,越界访问是 未定义的行为.在现代通用系统上,每个用户 进程具有其自己的地址空间,并且一个进程无法修改,甚至 读取另一个进程的进程(除非共享内存),所以除非您是 编写内核代码,您应该无法破坏任何东西 您自己的进程(非内核代码通常无法执行物理IO,因此 我看不到硬件中的任何东西都可能损坏).

It depends on your system. Formally, an out of bounds access is undefined behavior. On a modern general purpose system, each user process has its own address space, and one process can't modify, or even read, that of another process (barring shared memory), so unless you're writing kernel code, you shouldn't be able to break anything outside of your own process (and non-kernel code can't normally do physical IO, so I don't see how anything in the hardware could break).

但是,如果您正在编写内核代码或在嵌入式系统上工作 没有内存映射或保护的处理器,您可以从字面上看 超出范围破坏销毁硬件;如果程序是 控制核电厂之类的东西,甚至可以摧毁 不仅仅是运行代码的机器.

If you're writing kernel code, however, or working on an embedded processor with no memory mapping or protection, you can literally destroy hardware with an out of bounds write; if the program is controlling something like a nuclear power plant, you can even destroy a lot more than just the machine your code is running on.

这篇关于通过更改C ++中的内存损坏计算机的风险的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆