使用无效指针/内存地址:C++ (视窗)
我正在尝试编写一个变量监视类,该类允许我向它传递一个指针(最好是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试取消引用指向您可以解释的任何空间之外的指针几乎毫无意义。您可能访问的地址甚至可能没有映射到进程的内存空间中,因此实际上没有什么可看的。
当您的进程启动时,您实际上并没有 4 GB 可供使用。内存空间大小为 4 GB,但它主要由未映射到您的进程的孔组成。
最终这一切都取决于你在哪里获得你想要使用的指针。您通常可以考虑的内存地址可能来自:
malloc
或new
分配的范围内的任何内容,但尚未释放
d或删除
访问刚刚释放的内存块中的指针正是这种毫无意义的情况。一旦您释放内存,它很可能已经返回到操作系统,并且该地址不再映射到您的进程。
您可以在此处进一步了解此内容
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:
malloc
ornew
and not yetfree
d ordeleted
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