Search code examples
c++pointer-arithmetic

Adding number to pointer value


I am trying to add a number to a pointer value with the following expression:

&AddressHelper::getInstance().GetBaseAddress() + 0x39EA0; 

The value for the &AddressHelper::getInstance().GetBaseAddress() is always 0x00007ff851cd3c68 {140700810412032}

should I not get 0x00007ff851cd3c68 + 0x39EA0 = 7FF81350DB08 as a result?

while I am getting: 0x00007ff851ea3168 or sometimes 0x00007ff852933168 or some other numbers.

Did I took the pointer value incorrectly?


Solution

  • With pointer arithmetic, type is taken into account,

    so with:

    int buffer[42];
    char* start_c = reinterpret_cast<char*>(buffer);
    int *start_i = buffer;
    

    we have

    • start_i + 1 == &buffer[1]
    • reinterpret_cast<char*>(start_i + 1) == start_c + sizeof(int).
    • and (when sizeof(int) != 1) reinterpret_cast<char*>(start_i + 1) != start_c + 1

    In your case:

    0x00007ff851ea3168 - 0x00007ff851cd3c68) / 0x39EA0 = 0x08
    

    and sizeof(DWORD) == 8.