c++ - 关于引用和指向它们的指针的问题

标签 c++ reference

<分区>

Possible Duplicate:
pointer to a specific fixed address

int a, hex = 0x573285;
int * pA; // Pointer?
*pa = &a; // &a = the hexadecimal-memory location and *pa is now going to point to that place.

所以我想:

假设可以这样写,例如:

(*hex) = 5; or whatever the hexadecimal number is

改变内存的值?

如果我有一个程序(我不知道),它会显示我的游戏使用的所有内存位置。 如果我改变了程序中的值然后回到我的游戏或其他什么,它也会在那里改变吗? (如果游戏正在运行)。

(*this) 是否与 (this*) 相同?

编辑: 这行得通

int a = 5, b=0x28ff1c;
int* c = &a;
cout << *(c);

但不是:

int a = 5;
int * b=0x28ff1c;
int * c = &a;
cout << *(b);

也不是:

int a = 5, b=0x28ff1c;
int * c = &a;
cout << *(b);

你看到我想做什么了吗?

最佳答案

let's say it possible to write something like this for example:

(*hex) = 5; or whatever the hexadecimal number is

to change the value of the memory?

标准不一定保证这种能力,但在许多系统上你可以这样写:

*((int *) hex) = 5;

hex“转换”为指向int的指针,然后取消对该指针的引用并将int设置为5 — 当然,前提是 hex 中的值确实指的是您可以写入的某个内存位置。但不用说,您不应该在任何您实际打算使用做任何事情的程序中这样做。

if I had a program (I don't know one), that showed me all the memory locations my game use. if i changed the value in the program and then got back to my game or what ever, would it be changed there aswell? (if the game's running).

嵌入式系统通常是这种情况,但由于使用了 virtual memoryvirtual address spaces ,因此在具有普通操作系统的现代机器上通常不是这种情况。发生的情况是,一个进程通常不会知道它正在使用的内存的真实物理内存位置(如果有的话);相反,存储在指针中的值是“虚拟的”,并且依赖于进程。两个进程的 0x12345678 可以完全没有关系,被映射到完全独立的内存位置。

关于c++ - 关于引用和指向它们的指针的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14088227/

相关文章:

c# - 当方法采用对象 C# 时,字符串作为引用参数的问题

c# - 在 C# 中安全,在 C++ 中不安全,指针/引用的简单返回

c++ - stdio.h 头文件的详细引用

c++用变量决定数组大小

c++ - 如何使用static_cast来解析重载函数?

c++ - 有人可以帮我破译这些 typedef 行中的语法吗?

python - python中的循环引用

c++ - 抑制 MSVC 中 SetThreadName 异常的调试输出

c++ - 同步非常快的线程

java - 如何通过引用增加一个整数?