C++Mac OS X 10.11.2 vm_read给出错误的值
C++ Mac OS X 10.11.2 vm_read Giving Wrong Value
我正试图在mac操作系统上编写一个简单的内存读写器,但遇到了问题。
写入方面运行良好,甚至vm_read也返回KERN_SUCCCESS。但是,当我尝试打印我在vm_read中引用的缓冲区时,它会给我一个错误的值。
#include <iostream>
#include <mach/mach_init.h>
#include <mach/mach.h>
using namespace std;
int main() {
int start = 1337;
int value = 100;
uintptr_t buf;
uint32_t bytesRead;
task_t task;
task_for_pid(current_task(), getpid(), &task);
cout << "Step 1 (before write, should = 1337): " << start << endl;
int reason = 0;
if ((reason = vm_write(task, (uintptr_t) &start, (uintptr_t) &value, sizeof(int))) != KERN_SUCCESS) {
cout << "Failed to write: " << reason << endl;
}
if ((reason = vm_read(task, (uintptr_t) &start, sizeof(int), &buf, &bytesRead)) != KERN_SUCCESS) {
cout << "Failed to read: " << reason << endl;
}
cout << "Step 2 (after write, should = 100): " << buf << endl;
return 0;
}
以下代码输出:
/Users/Jonathan/Library/Caches/CLion12/cmake/generated/4f3c65b6/4f3c65b6/Debug/helloworld
Step 1 (before write, should = 1337): 1337
Step 2 (after write, should = 100): 4415766528
Process finished with exit code 0
步骤2应该打印100,因为我将值100写入起始变量。(如果我执行cout << start << endl;
,它将打印100)。
知道怎么了吗?谢谢
尝试将步骤2替换为:
cout << "Step 2 (after write, should = 100): " << (*(int*)buf) << endl;
Buf是指向您读取的数据的指针,因此您必须取消引用它才能从中获得实际的整数。
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 过载时出错 << "Exception thrown: read access violation.m.matrix was 0xCDDDCDDE."
- 使用stat和read()读取cpp文件时出错
- recv()/send()(在Socket中)或read()/write(在CFile中)中出错