How does a C++ std::container (vector) store its i

2019-05-25 06:59发布

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:

enter image description here

(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条回答
祖国的老花朵
2楼-- · 2019-05-25 07:51

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.

查看更多
登录 后发表回答