从另一个程序分配特定的内存地址,并更改它的值

发布于 2024-08-19 14:49:27 字数 1470 浏览 8 评论 0原文

最近我请假几天,想做一个用C++处理内存地址的小程序实验。

我想看到的是,当前正在运行的程序(我们称之为程序 A)创建了一个指向堆中 int 对象的指针,是否可以被另一个程序看到并被修改(程序 B)。

因此,对于程序 A,这是我的基本代码:

// Program A
#include <iostream>

using namespace std;

int main()
{
    // Pointer to an int object in the heap
    int *pint = new int(15);

    // Display the address of the memory in heap
    cout << pint << endl;

    // Display the value stored in that address
    cout << *pint << endl;

    return 0;
}

程序 A 的输出:

0x641030
15

对于程序 B,我查看了如何通过此链接分配特定的内存地址: http://www.devx.com/tips/Tip/14104

的代码程序 B 是:

// Program B
#include <iostream>

using namespace std;

int main()
{
    // assign address 0x641030 to p
    int *p = reinterpret_cast< int* > (0x641030);

    cout << p << endl;
    cout << *p << endl;

    return 0;
}

程序 B 的输出:

0x641030
... "Crash"

我不太明白。我原本期望 *p 显示 15,但它做了一些我没想到的事情。

我还尝试将 *p 分配给像 *p = 2000 这样的数字,但当我尝试这样做时它崩溃了。

另外,当我显示指针和程序 A (cout << &pint;) 和程序 B (cout << &p;) 的地址时>),它们都显示相同的内存地址。

有谁知道到底发生了什么事?我对正在发生的事情很感兴趣但又感到困惑。另外,我可以用 C++/C 做我尝试的事情吗?

** 编辑 ** 抱歉没有提及我的平台,但我目前使用的是 Window 7 Professional

Recently I've time off of school for a few days and wanted to do a small program(s) experiment in C++ dealing with memory address.

I wanted to see is that if a currently running program (Let call it Program A) that created a pointer to an int object in the heap, can be seen by another program and be modified (Program B).

So for Program A, this is my basic code:

// Program A
#include <iostream>

using namespace std;

int main()
{
    // Pointer to an int object in the heap
    int *pint = new int(15);

    // Display the address of the memory in heap
    cout << pint << endl;

    // Display the value stored in that address
    cout << *pint << endl;

    return 0;
}

Output for Program A:

0x641030
15

For Program B, I looked at how to assigned a specific memory address through this link:
http://www.devx.com/tips/Tip/14104

The code for Program B is:

// Program B
#include <iostream>

using namespace std;

int main()
{
    // assign address 0x641030 to p
    int *p = reinterpret_cast< int* > (0x641030);

    cout << p << endl;
    cout << *p << endl;

    return 0;
}

Output for Program B:

0x641030
... "Crash"

I don't quite understand it. I was expecting a display of 15 from *p, but it did something i didn't expect.

I also tried to assign *p to a number like *p = 2000 but it crashed when I attempted that as well.

Also when I display the address of the pointers and Program A (cout << &pint;) and for Program B (cout << &p;), they both showed the same memory address.

Does anyone know what is going on exactly? I'm interested yet confused of what is happening. Also, is it possible for me to do what I am attempt in C++/C ?

** EDIT **
Sorry to not mention my platform, but I am currently using Window 7 Professional

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七婞 2024-08-26 14:49:27

简而言之,不同的进程使用完全不同的地址空间。如果不做更多的工作,进程 B 就无法从进程 A 读取或写入内存。

可以通过特定于平台的方式来做到这一点。 Win32 提供了诸如 WriteProcessMemory 之类的函数,您可以直接在其中将值插入另一个进程的内存空间。大多数操作系统都提供共享内存功能,在 Win32 中您可以使用内存映射文件,而 Unix 风格通常具有某种等效的“mmap”或“shmem”功能。

The short answer is that different processes use completely different address spaces. Without doing a lot more work, process B can't read or write the memory from process A.

It is possible to do this, in a platform-specific way. Win32 offers functions such as WriteProcessMemory where you can directly poke values into the memory space of another process. Most operating systems offer a shared memory function, with Win32 you can use memory mapped files and Unix flavours typically have some kind of equivalent "mmap" or "shmem" feature.

月下客 2024-08-26 14:49:27

我认为大多数操作系统的设计都是为了让你不可能(或非常困难)做你想做的事情——让一个正在运行的程序(或进程)干扰另一个正在运行的程序(或进程)的地址空间内容)。由于您没有告诉我们您的平台,因此很难对此做出明确的判断,但我怀疑操作系统正在将您从自己手中拯救出来。这种严格的进程分离是单用户计算机上的安全功能,也是多用户计算机上的安全保障功能。

当然,有许多用于运行并发进程的技术,这些进程可以共享内存或通过消息传递交换信息。多花点时间离开学校去学习这些!

I think that most operating systems are designed to make it impossible (or very difficult) to do what you are trying to do -- have one running program (or process) interfere with the contents of the address space of another running program (or process). Since you don't tell us your platform it's difficult to be categorical about this, but I suspect that the o/s is saving you from yourself. This rigid separation of processes is a safety feature on single-user machines, a safety and security feature on multi-user machines.

There are, of course, many techniques for running concurrent processes which can share memory or which exchange information by message-passing. Take some more time off school and study those!

猥琐帝 2024-08-26 14:49:27

查看内存映射文件或共享内存。

http://msdn.microsoft.com/en-us /library/aa366551(VS.85).aspx

Take a look at either Memory Mapped Files or Shared Memory.

http://msdn.microsoft.com/en-us/library/aa366551(VS.85).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文