C ++:我可以用指针离开我的应用程序的内存边界吗? [英] C++: Can I get out of the bounds of my app's memory with a pointer?
问题描述
如果我有一些愚蠢的代码,像这样:
If I have some stupid code like this:
int nBlah = 123;
int* pnBlah = &nBlah;
pnBlah += 80000;
*pnBlah = 65;
我可以更改其他应用程式的记忆吗?
Can I change another app's memory?
你解释我这是邪恶的,我知道。但我只是有兴趣。
这不是简单的尝试。我不知道会发生什么。
You have explained me this is evil, I know. But I was just interested.
And this isn't something to simply try. I don't know what would happen.
感谢
推荐答案
在C ++术语中,这是未定义的行为。实际上会发生什么取决于许多因素,但最重要的是它取决于您使用的操作系统(OS)。在现代内存管理的操作系统上,您的应用程序将被终止,并尝试访问进程地址空间之外的内存时出现分段故障(实际术语取决于操作系统)。然而,一些操作系统没有这种保护,你可以肆意捅,摧毁属于其他程序的东西。如果你的代码在内核空间中,通常也是这种情况。在设备驱动程序中。
In C++ terms, this is undefined behavior. What will actually happen depends on many factors, but most importantly it depends on the operating system (OS) you are using. On modern memory-managed OS's, your application will be terminated with a "segmentation fault" (the actual term is OS-dependent) for attempting to access memory outside of your process address space. Some OS's however don't have this protection, and you can willy-nilly poke and destroy things that belong to other programs. This is also usually the case if your code is inside kernel space, e.g. in a device driver.
这篇关于C ++:我可以用指针离开我的应用程序的内存边界吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!