C++无法写入虚拟存储器尽管作为读写
问题描述:
我无法写入虚拟存储器块,尽管它被标记为PAGE_READWRITE被标记。这是在运行时抛出的异常:C++无法写入虚拟存储器尽管作为读写
Exception thrown at 0x722E4AE9 (vcruntime140d.dll) in ConsoleApplication.exe: 0xC0000005: Access violation writing location 0x00000000.
下面是代码:
#include <iostream>
#include <windows.h>
int main() {
char* memblock;
memblock = new char[8] { 0, 1, 2, 3, 4, 5, 6, 7 };
LPVOID virtual_memory_area = VirtualAlloc(NULL, 8, MEM_PHYSICAL, PAGE_READWRITE);
memcpy(virtual_memory_area, memblock, 8);
}
答
的VirtualAlloc
的flAllocationType
参数必须具有的MEM_COMMIT
一个,MEM_RESERVE
,MEM_RESET
或MEM_RESET_UNDO
。该MEM_PHYSICAL
是可选的标志flAllocationType
。
你只有MEM_PHYSICAL
,因此VirtualAlloc
失败。
答
如果你看一下错误,您正在试图写一个空指针,因此除外。原因是VirtualAlloc失败并返回null。
的文件说,当错误发生时它的VirtualAlloc返回null,设置错误。也许这个帮助你。 – Hayt
希望Hayt说。我真诚怀疑您的系统上的“位置0x00000000”是可读写的。 –