使用十六进制内存地址获取变量的值

时间:2022-03-02 21:43:40

I just want to get the value using the hex address:

我只想使用十六进制地址获取值:

#include<stdio.h>

int main()
{
   int *hold, value=10;
   hold = &value;
   printf("%p",hold);
}

It gives me the address 0x7fffd7c24334. I just want to know that if there is a way in C or C++ so that I can get directly the value of "value" using hex number 0x7fffd7c24334 for the time being. I suppose the address of the variable hold is the same for sometime so that 0x7fffd7c24334 still point to value, but I am not sure.

它给了我地址0x7fffd7c24334。我只想知道如果在C或C ++中有一种方法,那么我可以暂时使用十六进制数0x7fffd7c24334直接得到“value”的值。我想变量hold的地址有时是相同的,所以0x7fffd7c24334仍然指向值,但我不确定。

2 个解决方案

#1


4  

Assuming that this address is consistent upon every execution of your program, you can use this:

假设每次执行程序时此地址都是一致的,您可以使用:

int value2 = *(int*)0x7fffd7c24334;

But please note that this assumption is generally wrong!

但请注意,这种假设通常是错误的!

The address of a local variable in a function depends on the state of the stack (the value of the SP register) at the point in execution when the function is called.

函数中局部变量的地址取决于调用函数时执行点的堆栈状态(SP寄存器的值)。

It might possibly work in main, since this function is called only once, and the SP register should have the same value upon every execution of your program. But even if it does work, that address will change as soon as you add variables before value. To put it in simple words, don't use this method.

它可能在main中工作,因为此函数只被调用一次,并且SP寄存器在每次执行程序时都应具有相同的值。但即使它确实有效,只要在值之前添加变量,该地址就会发生变化。用简单的话来说,不要使用这种方法。

#2


0  

printf("%d",*hold);

De-reference and use %d.

取消引用并使用%d。

You cannot rely on the hex address.

您不能依赖十六进制地址。

See here: http://ideone.com/RdY4Ni

见这里:http://ideone.com/RdY4Ni

#1


4  

Assuming that this address is consistent upon every execution of your program, you can use this:

假设每次执行程序时此地址都是一致的,您可以使用:

int value2 = *(int*)0x7fffd7c24334;

But please note that this assumption is generally wrong!

但请注意,这种假设通常是错误的!

The address of a local variable in a function depends on the state of the stack (the value of the SP register) at the point in execution when the function is called.

函数中局部变量的地址取决于调用函数时执行点的堆栈状态(SP寄存器的值)。

It might possibly work in main, since this function is called only once, and the SP register should have the same value upon every execution of your program. But even if it does work, that address will change as soon as you add variables before value. To put it in simple words, don't use this method.

它可能在main中工作,因为此函数只被调用一次,并且SP寄存器在每次执行程序时都应具有相同的值。但即使它确实有效,只要在值之前添加变量,该地址就会发生变化。用简单的话来说,不要使用这种方法。

#2


0  

printf("%d",*hold);

De-reference and use %d.

取消引用并使用%d。

You cannot rely on the hex address.

您不能依赖十六进制地址。

See here: http://ideone.com/RdY4Ni

见这里:http://ideone.com/RdY4Ni