0

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?

17
  • 2
    Pointer arithmetic take into account type of the pointer... Commented Oct 6, 2021 at 10:14
  • 1
    (0x00007ff851ea3168 - 0x00007ff851cd3c68) / 0x39EA0 = 0x08, So I suspect it is the size of your struct/type. Commented Oct 6, 2021 at 10:21
  • 2
    @Kaihaku: See Pointer_arithmetic. it is pedantically UB, even if it might works in practice for most compiler. Commented Oct 6, 2021 at 10:26
  • 1
    @Kaihaku The standard decides what is and isn’t UB. Saying that something is “UB on an obscure microcontroller” doesn’t make sense. Commented Oct 6, 2021 at 10:31
  • 1
    Pedantically, you cannot have/use arbitrary adresses. Then compiler might decide what to do for some UB, but it is no longer inside C++ rules. Platform might provide "buffer" at specific address. Notice also that some compilers remove code which leads to UB. Commented Oct 6, 2021 at 10:41

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.