使用无效指针/内存地址:C++ (视窗)

发布于 2024-10-27 00:25:49 字数 468 浏览 1 评论 0原文

我正在尝试编写一个变量监视类,该类允许我向它传递一个指针(最好是 void*),该指针寻址通常完全超出该类范围或无法访问的内存位置。然后,该类会定期在屏幕上以文本形式显示该内存位置的内容 - 以用户定义的方式解释(例如 (int*) )。我只会使用这个指针从内存中读取数据,它将作为一种肮脏的黑客行为,在开发过程中为我暂时感兴趣的运行时监控变量启用一种监视窗口 - 无需引入大量代码这些变量在类的范围内/可访问。

我正在使用 VC++ 2010,它似乎完全拒绝让我向指针写入超出范围的内存位置地址。

我猜想 Windows 中发生了很多事情,因此随着内存位置的变化,这种方法的适用性可能非常有限,但我使用的是本机 C++,所以我希望我的地址足够持久以有用。另外,我可以看到它不希望我访问我的程序出于安全原因实际上并未使用的内存位置......

我有什么想法可以做到这一点吗? (我意识到使用此类指针会导致未定义的行为,因此只会从它们中读取并显示值)。

谢谢。

I am trying to write a variable monitoring class that allows me to pass it a pointer (ideally void*) addressing a memory location which would normally be completely out-of-scope or inaccessible for the class. The class would then periodically display on the screen in text the contents of that memory location - interpreted in a user defined way (eg. (int*) ). I would only ever read from memory using this pointer and it would serve as a dirty hack to enable a kind of watch window during development for the variables that I am temporarily interested in monitoring during run-time - without introducing a lot of code to bring these variables in scope / accessible to the class.

I am using VC++ 2010 and it seems to flat out refuse to let me even write an out of scope memory location address to the pointer.

I guess there's a lot going on under the hood in windows such that this approach may have very limited applicability as memory locations change but I am using native C++ so am hoping that my addresses are persistent enough to be useful. Also, I can see that it would not like me accessing a memory location that my program is not actually using for security reasons...

Any ideas how I can do this? (I realise that using such pointers gives rise to undefined behaviour so would only ever read from them and display the value).

Thanks.

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

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

发布评论

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

评论(1

§普罗旺斯的薰衣草 2024-11-03 00:25:49

尝试取消引用指向您可以解释的任何空间之外的指针几乎毫无意义。您可能访问的地址甚至可能没有映射到进程的内存空间中,因此实际上没有什么可看的。
当您的进程启动时,您实际上并没有 4 GB 可供使用。内存空间大小为 4 GB,但它主要由未映射到您的进程的孔组成。

最终这一切都取决于你在哪里获得你想要使用的指针。您通常可以考虑的内存地址可能来自:

  • 堆分配 - 由mallocnew分配的范围内的任何内容,但尚未释放d或删除
  • 堆栈空间、全局变量 - 您在程序中当前位置范围内定义为程序中的变量的任何内容。访问其他作用域中定义的任何内容是没有意义的(例如,从函数返回指向局部变量的指针)
  • 代码段 - 包含未卸载的进程的 DLL 或 EXE 的内存段内的地址。通常您只能以只读方式访问它们。例如,您可以通过查找函数的返回地址来找到此类地址。

访问刚刚释放的内存块中的指针正是这种毫无意义的情况。一旦您释放内存,它很可能已经返回到操作系统,并且该地址不再映射到您的进程。

您可以在此处进一步了解此内容

Trying to dereference pointers that point outside any space which you can account for is pretty much meaningless. The address you may be accessing might not even be mapped into the memory space of your process so there is actually nothing even to look at.
When your process starts, You don't actually have 4 GB at your disposal. the memory space size is 4 GB but it is mostly made of holes that are not mapped to your process.

Eventually it all comes down to where you got the pointer you're trying to use. Memory addresses which you usually can account for may come from:

  • heap allocations - anything inside the ranges allocated by malloc or new and not yet freed or deleted
  • stack space, global variables - anything you define as variables in your program inside the scopes of your current position in the program. Accessing anything defined in other scopes is meaningless (for instance, returning a pointer to a local variable from a function)
  • code segments - addresses inside the segments of memory that contain the DLL or EXE of your process that were not unloaded. Usually you can access them only for read-only access. You can find such addresses for instance by looking up the return address of a function.

Accessing a pointer in a memory chunk you just deallocated is exactly a case of such meaninglessness. Once you deallocated your memory, there is a certain chance that it was already returned to the OS and that that address is no longer mapped to your process.

You can further read about this here

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