结构不在内存中 [英] Structure not in memory

查看:67
本文介绍了结构不在内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个这样的结构:

I created a structure like that:

struct Options {
    double bindableKeys = 567;
    double graphicLocation = 150;
    double textures = 300;
};
Options options;

此声明之后,在另一个进程中,我打开包含该结构的进程,并搜索具有该结构的double的字节数组,但未找到任何内容.

Right after this declaration, in another process, I open the process which contains the structure and search for a byte array with the struct's doubles but nothing gets found.

要获得结果,我需要在声明后添加类似std::cout << options.bindableKeys;的内容.然后我从模式搜索中得到结果. 为什么这样表现?有什么解决办法吗?

To obtain a result, I need to add something like std::cout << options.bindableKeys;after the declaration. Then I get a result from my pattern search. Why is this behaving like that? Is there any fix?

最小的可复制示例:

struct Options {
    double bindableKeys = 567;
    double graphicLocation = 150;
    double textures = 300;
};
Options options;
while(true)  {
    double val = options.bindableKeys;
    if(val > 10)
        std::cout << "test" << std::endl;
}

您可以使用CheatEngine或其他模式查找器搜索数组

You can search the array with CheatEngine or another pattern finder

推荐答案

与流行的看法相反,C ++源代码不是提供给执行计算机的指令序列.这不是可执行文件将包含的内容的列表.

Contrary to popular belief, C++ source code is not a sequence of instructions provided to the executing computer. It is not a list of things that the executable will contain.

这仅仅是对程序的描述.

您的编译器负责创建可执行程序,该程序遵循与源代码中所述相同的语义逻辑叙述.

Your compiler is responsible for creating an executable program, that follows the same semantics and logical narrative as you've described in your source code.

创建一个Options实例很好,但是如果创建它不做任何事情(没有副作用)并且您从不使用其任何数据,那么它也可能不存在,因此不存在该程序的逻辑叙述的一部分.

Creating an Options instance is all well and good, but if creating it does not do anything (has no side effects) and you never use any of its data, then it may as well not exist, and therefore is not a part of the logical narrative of your program.

因此,编译器没有理由将其放入可执行程序中.所以,事实并非如此.

Consequently, there is no reason for the compiler to put it into the executable program. So, it doesn't.

有人将其称为优化".该实例已被优化".我更喜欢将其称为常识:实例从不确实是程序的一部分.

Some people call this "optimisation". That the instance is "optimised away". I prefer to call it common sense: the instance was never truly a part of your program.

即使您确实在实例中使用了数据,也可能会创建一个更直接使用该数据的可执行程序.在您的情况下,不会更改Option成员的默认值,因此没有理由将其包含在程序中:if语句可以将567放入其中.然后,由于已经烤好,所以整个条件成为常数表达式567 > 10,该常数必须始终为true;您可能会发现生成的可执行程序因此根本不包含分支逻辑 .它只是启动,然后一遍又一遍地输出"test",直到您强制终止它为止.

And even if you do use the data in the instance, it may be possible for an executable program to be created that more directly uses that data. In your case, nothing changes the default values of Option's members, so there is no reason to include them into the program: the if statement can just have 567 baked into it. Then, since it's baked in, the whole condition becomes the constant expression 567 > 10 which must always be true; you'll likely find that the resulting executable program consequently contains no branching logic at all. It just starts up, then outputs "test" over and over again until you force-terminate it.

话虽这么说,因为我们生活在一个受物理定律支配的世界中,并且由于编译器不完善,所以这种抽象总是会出现 some 轻微泄漏的情况.因此,您可以欺骗编译器,以使实例被使用",要求实例的形式必须在可执行文件中更正式地表示,即使实现所描述的程序不是必需的. 这在基准测试代码中很常见.

That all being said, because we live in a world governed by physical laws, and because compilers are imperfect, there is always going to be some slight leakage of this abstraction. For this reason, you can trick the compiler into thinking that the instance is "used" in a way that requires its presence to be represented more formally in the executable, even if this isn't necessary to implement the described program. This is common in benchmarking code.

这篇关于结构不在内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆