2014-10-18 36 views
0

(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

unsigned long类型是MSVC 32位,所以可以”拿着一个地址。使用保证与指针大小相同的uintptr_t。

+0

谢谢,现在我没有得到错误,它的工作:) – Matthew 2014-10-18 20:48:33