简体   繁体   English

具有记忆的C样式转换

[英]C-Style Cast With Memory

Here is an example of a C-Style cast that I am using: 这是我正在使用的C风格演员表的示例:

*(char*)(0x0FCA41E + (0x280 * i)) = 0xFF;

This code is essentially a Call of Duty hack which tampers with the health of the player. 该代码本质上是一个“使命召唤”黑客,会篡改玩家的健康状况。 By default, the value at address 0x0FCA41E is 0x00 but changing it to 0xFF initiates infinate health. 默认情况下,地址0x0FCA41E值为0x00但将其更改为0xFF启动无限运行状况。 I understand what this line of code does, but does this have to be a char ? 我知道这行代码的作用,但这必须是char吗? Can it look like this? 可以这样吗?

*(int*)(0x0FCA41E + (0x280 * i)) = 0xFF;
//or
*(short*)(0x0FCA41E + (0x280 * i)) = 0xFF;

I understand that 0xFF is one byte of memory, like a char , but what would happen if I used an int or a short ? 我知道0xFF是一个字节的内存,就像char ,但是如果我使用intshort会发生什么呢? Would that work? 那行得通吗?

If not, then what would be an example of using an int or short in this sense? 如果不是,那么在这种意义上使用intshort的例子将是什么?

Thanks! 谢谢!


By the way, I made a similar post to this a few days go (incase it looks familiar) but this is a different question overall. 顺便说一句,我几天前也发表了类似的帖子(以防万一,它看起来很熟悉),但这总体上是一个不同的问题。

In addition to what iharob said, another difference is how much memory the assignment will overwrite. 除了iharob所说的以外,另一个区别是分配将覆盖多少内存。 In the example, using a char will overwrite a single byte with 0xFF. 在示例中,使用char将用0xFF覆盖单个字节。 But if you cast it to an int, then 4 or 8 bytes (depending on the machine architecture) will be overwritten. 但是,如果将其强制转换为int,则将覆盖4或8个字节(取决于计算机体系结构)。 This will result in some undefined behavior as you don't know what other values, if any, will be erased. 这将导致一些未定义的行为,因为您不知道会擦除其他哪些值(如果有)。

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

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