试图从地址中获取值会导致访问冲突



(Windows 7 x64)

我在内存中存储了一些值,并创建了一个函数来返回该地址。但当我试图获得价值时,它会给我

First-chance exception at 0x000000013fbc1b2b in Testing.exe: 0xC0000005: Access violation reading location 0x000000003fbca000.
Unhandled exception at 0x000000013fbc1b2b in Testing.exe: 0xC0000005: Access violation reading location 0x000000003fbca000.
The program '[2528] Testing.exe: Native' has exited with code -1073741819 (0xc0000005).

C++:

#include <iostream>
extern "C" unsigned long getMemoryAddress();
extern "C" unsigned long getValue(unsigned long address);
int main() {
    unsigned long address = getMemoryAddress();
    unsigned long value = getValue(address);
    std::cout << "Address: " << address << std::endl;
    std::cout << "Value: " << value << std::endl;
    return 0;
}

来自组装

    .data
    memory db 15
.code
    getMemoryAddress proc
        lea rax, memory ; Get address to return
        ret
    getMemoryAddress endp
    getValue proc
        mov rax, [rcx] ; Get value from address
        ret
    getValue endp
end

unsigned long在MSVC中是32位的,因此无法保存地址。使用uintptr_t,它保证与指针大小相同。

最新更新