Reverse Engineering for Beginners

(avery) #1

CHAPTER 51. C++ CHAPTER 51. C++


ptr=0x003CC288 _Next=0x003CC270 _Prev=0x003CC258 x=3 y=4
ptr=0x003CC270 _Next=0x003CC2A0 _Prev=0x003CC288 x=1 y=2
ptr=0x003CC2A0 _Next=0x003CC258 _Prev=0x003CC270 x=5 y=6
node at .begin:
ptr=0x003CC288 _Next=0x003CC270 _Prev=0x003CC258 x=3 y=4
node at .end:
ptr=0x003CC258 _Next=0x003CC288 _Prev=0x003CC2A0 x=6226002 y=4522072



  • let's count from the begin:
    1st element: 3 4
    2nd element: 1 2
    3rd element: 5 6
    element at .end(): 6226002 4522072

  • let's count from the end:
    element at .end(): 6226002 4522072
    3rd element: 5 6
    2nd element: 1 2
    1st element: 3 4
    removing last element...
    _Myhead=0x003CC258, _Mysize=2
    ptr=0x003CC258 _Next=0x003CC288 _Prev=0x003CC270 x=6226002 y=4522072
    ptr=0x003CC288 _Next=0x003CC270 _Prev=0x003CC258 x=3 y=4
    ptr=0x003CC270 _Next=0x003CC258 _Prev=0x003CC288 x=1 y=2


C++11 std::forward_list


The same thing as std::list, but singly-linked one, i.e., having only the “next” field at each node. It has a smaller memory
footprint, but also don’t offer the ability to traverse list backwards.


51.4.3 std::vector


We would callstd::vectora safe wrapper of thePODT^8 C array. Internally it is somewhat similar tostd::string
(51.4.1 on page 538): it has a pointer to the allocated buffer, a pointer to the end of the array, and a pointer to the end of
the allocated buffer.


The array’s elements lie in memory adjacently to each other, just like in a normal array (18 on page 253). In C++11 there is
a new method called.data(), that returns a pointer to the buffer, like.c_str()instd::string.


The buffer allocated in theheapcan be larger than the array itself.


Both MSVC’s and GCC’s implementations are similar, just the names of the structure’s fields are slightly different^9 , so here is
one source code that works for both compilers. Here is again the C-like code for dumping the structure ofstd::vector:


#include <stdio.h>
#include
#include
#include


struct vector_of_ints
{
// MSVC names:
int Myfirst;
int
Mylast;
int *Myend;


// GCC structure is the same, but names are: _M_start, _M_finish, _M_end_of_storage
};


void dump(struct vector_of_ints *in)
{
printf ("_Myfirst=%p, _Mylast=%p, _Myend=%p\n", in->Myfirst, in->Mylast, in->Myend);
size_t size=(in->Mylast-in->Myfirst);
size_t capacity=(in->Myend-in->Myfirst);
printf ("size=%d, capacity=%d\n", size, capacity);
for (size_t i=0; i<size; i++)
printf ("element %d: %d\n", i, in->Myfirst[i]);


(^8) (C++) Plain Old Data Type
(^9) GCC internals:http://go.yurichev.com/17086

Free download pdf