简体   繁体   English

指针偏移量有什么好处?

[英]What is the benefit of offsets to pointers?

I'm using a memory editing application known as Cheat Engine. 我正在使用一个称为作弊引擎的内存编辑应用程序。 I attach Cheat Engine to a game. 我将作弊引擎附加到游戏上。 In my game, I have a 32-bit integer known as HP. 在我的游戏中,我有一个32位整数,称为HP。 HP is stored at a memory address A. If I restart my game, HP is stored at a new memory address B. It seems that using Cheat Engine, I can do a pointer scan and find a static memory address, C, that points to another memory address and its accompanying offset, D and offset, so that [D + offset] always stores HP's memory address during that session. HP存储在内存地址A中。如果我重新启动游戏,HP存储在新的内存地址B中。看来,使用作弊引擎,我可以进行指针扫描并找到一个静态内存地址C,该地址指向另一个内存地址及其伴随的偏移量D和偏移量,以便[D +偏移量]在该会话期间始终存储HP的内存地址。 So if I dereference [D + offset], I always get the memory address that stores HP. 因此,如果取消引用[D +偏移量],则始终可以获得存储HP的内存地址。

Here is a diagram: 这是一个图:

A or B --> HP A或B-> HP

D + offset --> A or B D +偏移量-> A或B

C --> D C-> D

What is the benefit of using offsets? 使用偏移量有什么好处? Why can't C just point to A or B directly? 为什么C不能直接指向A或B? I'm familiar that using offsets are beneficial when dealing with arrays in the C language. 我知道在使用C语言处理数组时使用偏移量是有益的。 Does that mean that whenever I see an offset to a pointer, the pointer is pointing to the first element in an array and the offset is referring to one of the elements in the array? 这是否意味着每当我看到指针的偏移量时,指针就指向数组中的第一个元素,而偏移量指向数组中的一个元素?

It should be easy to imagine and understand if you know the C programming language. 如果您知道C编程语言,应该容易想象和理解。 Anything you write in C is very close to the actual machine code that gets generated when compiling the program. 您用C编写的任何内容都非常接近在编译程序时生成的实际机器代码。

The abstraction of an object in C is often done with a "struct". C语言中对象的抽象通常是通过“结构”完成的。

In your example imagine a very simple "Player" struct: 在您的示例中,想象一个非常简单的“ Player”结构:

struct Player {
    int id;
    float xposition;
    float yposition;
    int health;
    int maxhealth;
};

If you want to create an object like this you could do something like this: 如果要创建这样的对象,可以执行以下操作:

struct Player *myPlayer = malloc(sizeof(struct Player));

What is a nice looking structured thing in the high language is actually just a block of memory in a compiled program. 用高级语言看起来不错的结构化内容实际上只是编译程序中的一块内存。

To access for example "health" you do myPlayer->health; 要访问例如“健康”,请执行myPlayer->health; in C. But now how does this look in a compiled program that doesnt know about the beatiful names and just has a block of memory to work with? 但是现在在不知道漂亮名称并且只有一块内存可以使用的已编译程序中它看起来如何?

It has to work with offsets from the base pointer. 它必须使用与基本指针的偏移量。 In the above example (assuming windows operating system and any default configured sane compiler) an access in some pseudo machine code will look something like this: 在上面的示例中(假设Windows操作系统和任何默认配置的健全编译器),对某些伪机代码的访问将类似于以下内容:

move myHealth, read4bytes[myPlayer + 12]

If you reverse-engineer a progam, you can't tell from an offset-access whether the block of memory was a struct, a array, or maybe a class (from C++ code) or something entirely different. 如果对程序进行反向工程,则无法通过偏移量访问来判断内存块是结构,数组还是类(来自C ++代码)还是完全不同的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM