试图从地址获取值会给我一个访问冲突?

时间:2014-10-18 05:54:25

标签: c++ visual-studio masm

(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

1 个答案:

答案 0 :(得分:1)

无符号长整数在MSVC中为32位,因此无法保存地址。使用保证与指针大小相同的uintptr_t。