NO

Author Topic: Stupid question about pointers  (Read 1599 times)

Offline bitcoin

  • Member
  • *
  • Posts: 179
Stupid question about pointers
« on: March 01, 2021, 01:55:00 PM »
Hello, I can't understand

Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int e_p()
{
LPBYTE PE = HeapAlloc(GetProcessHeap(),0,1024);
DWORD *pointer,*pointer2 = 0;


if(PE != NULL)
{
pointer = (DWORD*)(PE + 0xC);
pointer2 = (DWORD*)(PE + 0xC); //pointer 2 equal pointer
pointer2 = (DWORD*)PE + 0xC; //no equal!
}

ExitProcess(0);
}

Why pointer != pointer2 in last line?  ??? I thought it would be equal, but no.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Stupid question about pointers
« Reply #1 on: March 01, 2021, 04:27:16 PM »
Because in
Code: [Select]
pointer2 = (DWORD*)(PE + 0xC);'PE' is a byte pointer, to which you add 0xC and by the pointers arithmetic you get 'PE = PE + (sizeof(BYTE) * 0xC)'.
Then you cast 'PE' to a pointer to 'DWORD'.
In the second case
Code: [Select]
pointer2 = (DWORD*)PE + 0xC;First you cast 'PE' to a 'DWORD' pointer then add the offset. In this case the pointers arithmetic works differently: 'PE = PE + (sizeof(DWORD) * 0xC)'. And because the size of a DWORD=4, you're adding 0xC*4.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline bitcoin

  • Member
  • *
  • Posts: 179
Re: Stupid question about pointers
« Reply #2 on: March 01, 2021, 05:20:57 PM »
Thank you frankie !