C ++ std :: container(vector)如何存储其内部(元素地址,通过索引访问)? [英] How does a C++ std::container (vector) store its internals (element address, access by index)?
问题描述
我试图攻击一个游戏(红色警报3),我尝试做一个程序,显示我的选手的单位列表。至于我首先需要找到一个(静态)指针到我自己的列表,我可以在单人游戏。
I am trying to "hack" a game (Red Alert 3), I try to make a program which shows the unit list of my oponents. As for that I first need to find a (static) pointer to my own list which I can do on singleplayer.
我注意到这种行为:地址由add_unit代码更改):
I have noticed this behaviour: (by looking at which addresses are changed by the add_unit code):
- 如果单位尚未构建,请为其创建一个新地址),并将该值设置为1(该类型的单位数量)
- 当游戏中已经构建单位时,将单位类型的原始地址增加1
-
- if a units hasn't been build yet, create a new adress for it (random?) and set the value to 1 (amount of units of that type)
- when the unit has been already build once in the game, increment the original adress of the unit type by 1
这看起来像std :: vector行为。现在我有麻烦找到矢量的基地地址,和一个更大的问题:我将如何通过索引访问?
This looks to me like std::vector behaviour. Now I am having trouble to find the "base" adress of the vector, and a bigger problem: How would I access by index? Where does a std::vector store it's adresses it has for elements?
额外信息:
代码是用MS Visual C ++ 2005(需要播放MSVCR80 dll)编译的(从我从程序集中读取的)
The code is (from what I have read from the assembly) compiled with MS Visual C++ 2005 (MSVCR80 dll's are required to play)
这是矢量中的地址看起来像:
This is what the addresses in the vector look like:
>
(突出显示的地址是显示为第一个元素 - 第一个单元构建的地址)
(The highlighted address is the one which appeared as the first element - first unit build)
这看起来不像我可以通过添加一个常量值来迭代?
This doesn't look like I could iterate by adding a constant value?
每当添加一个新地址时,所有其他地址都是完全有效的,不会改变。
Whenever a new address is added, all the other addresses are perfectly valid and don't change.
推荐答案
向量
的一个典型的(虽然不是强制性的) :
A typical (though by no means mandatory) implementation of vector
is to have three consecutive words:
struct TypicalVector
{
T * start;
T * end;
T * capacity;
};
元素访问通过 start [i]
(这是为什么在前面有开始
指针很重要,以避免不必要的偏移计算),大小是 end-start
,容量为 capacity-start
。内存分配获得 c * sizeof(T)
字节,并将 start
设置为已分配内存的地址, capacity
到 start + c
。元素构造递增结束
。
Element access is done via start[i]
(which is why it's important to have the start
pointer at the front, to avoid unnecessary offset computations), size is end - start
, and capacity is capacity - start
. Memory allocation obtains c * sizeof(T)
bytes and sets start
to the address of the allocated memory and capacity
to start + c
. Element construction increments end
.
这篇关于C ++ std :: container(vector)如何存储其内部(元素地址,通过索引访问)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!