How does a C++ std::container (vector) store its internals (element address, access by index)?

青春壹個敷衍的年華 提交于 2019-12-07 19:39:26

问题


I am trying to "hack" a game (Red Alert 3), I try to make a program which shows the unit list of my opponents. As for that I first need to find a (static) pointer to my own list which I can do on single player.

I have noticed this behaviour: (by looking at which addresses are changed by the add_unit code):

  • if a units hasn't been build yet, create a new address 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 address of the unit type by 1

This looks to me like std::vector behaviour. Now I am having trouble to find the "base" address of the vector, and a bigger problem: How would I access by index? Where does a std::vector store it's addresses it has for elements?

Extra info:

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.


回答1:


A typical (though by no means mandatory) implementation of vector is to have three consecutive words:

struct TypicalVector
{
    T * start;
    T * end;
    T * capacity;
};

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.



来源:https://stackoverflow.com/questions/15701597/how-does-a-c-stdcontainer-vector-store-its-internals-element-address-acc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!