title: Better ways to reference multi-level pointers?
body: <p>As for now I'm trying to make a basic "fly mode" for a game where such one does not exist. For that I need to manipulate the game memory, and that means that pointers will have to be used to access the memory (from withing a DLL).</p>
  
  <p>As for now I use this code:</p>
  
  <pre><code>#define AddVar(Type,Name,Address) Type&amp; Name = *reinterpret_cast&lt;Type*&gt;(Address)
  AddVar(unsigned int,gameBaseAddress,0x0089CDA8);//base address
  
  //*Reveal* pointer, dunno how to name this
  #define RevPointer(addr,type) (*(type *)(addr))
  
  void FrameTick(IDirect3DDevice9 * device, HWND wnd)
  {
      DirectXFont::Access(0)-&gt;Print(0.0,0.0,0xFFFFFFFF,"{FFFF0000}N{FF00FF00}F{FF0000FF}S {FFFFFF00}MOD {FF00FFFF}1.0",true);
      if(gameBaseAddress)//check if the game has loaded &lt;accessing the VALUE at gameBaseAddress)
      {
              //lvl 1 ptr
          unsigned int BaseAddr = RevPointer(gameBaseAddress+0x20,unsigned int);//base + pointer offset 1 (+0x20)
  
              //lvl2 ptr
          BaseAddr += 0x20;//(base + offset 1 (+0x20)) + pointer offset 2 (+0x20) - X pos
          unsigned int PosXAddr = RevPointer(&amp;BaseAddr,unsigned int);//got this line of code by trial and error, don't know how this magicly works
          BaseAddr += 0x4;//y pos is 4 bytes further
          unsigned int PosYAddr = RevPointer(&amp;BaseAddr,unsigned int);
          BaseAddr += 0x4;//z the same
          unsigned int PosZAddr = RevPointer(&amp;BaseAddr,unsigned int);
  
          float *PosX = (float*)PosXAddr;
          float *PosY = (float*)PosYAddr;
          float *PosZ = (float*)PosZAddr;
          if(PosXAddr &amp;&amp; PosXAddr &lt; 0xAAAA0000)//check if pointer is valid
          {
              DirectXFont::Access(0)-&gt;Print(0.0,60.0,0xFFFFFFFF,string_format("%.2f %.2f %.2f",*PosX,*PosY,*PosZ).c_str(),true);//works
          }
      }
  }
  </code></pre>
  
  <p>From debugging thegame I found out that all pointers are.. well.. multi level pointers:</p>
  
  <p>(((base_adderss + 0xOFFSET1) + 0xOFFSET2) + 0xOFFSET3)...</p>
  
  <p>However this way, it will become very inconvienient to manage all these adresses and offsets.</p>
  
  <p>What I'm doing here in this code is accessing the game's base address, add the first offset to the pointer, then from that pointer I pply the X Y Z offsets (+0x20 , +0x24, +0x28) to get the XYZ position of the wanted object from memory.</p>
  
  <p>This code looks already ugly..
  Are there better ways to accomplish what I want to do?</p>
  
  <hr>
  
  <p>Thank you all for the input. If anyone wants the current code I have:</p>
  
  <pre><code>#define AddVar(Type,Name,Address) Type&amp; Name = *reinterpret_cast&lt;Type*&gt;(Address)
  AddVar(unsigned int,PositionBaseAddress,0x0089CDA8);//base address
  
  struct Point { float x, y, z; };
  
  void FrameTick(IDirect3DDevice9 * device, HWND wnd)
  {
      DirectXFont::Access(0)-&gt;Print(0.0,0.0,0xFFFFFFFF,"{FFFF0000}N{FF00FF00}F{FF0000FF}S {FFFFFF00}MOD {FF00FFFF}1.0",true);
      if(PositionBaseAddress)
      {
          auto BaseAddr = *(unsigned int*)(PositionBaseAddress + 0x20);
          if(IsBadReadPtr(&amp;BaseAddr,0x04) != 0)
              return;
          auto&amp; p = *(Point*)(BaseAddr + 0x20);
          auto&amp; v = *(Point*)(BaseAddr + 0x70);
          if(IsBadReadPtr(&amp;p,0x04) != 0)
              return;
          DirectXFont::Access(0)-&gt;Print(0.0,45.0,0xFFFFFFFF,string_format("Position: %.2f %.2f %.2f",p.x,p.y,p.z).c_str(),true);
          DirectXFont::Access(0)-&gt;Print(0.0,60.0,0xFFFFFFFF,string_format("Velocity: %.2f %.2f %.2f",v.x,v.y,v.z).c_str(),true);
      }
  }
  
  /*
  Position.X: (0x0089CDA8 + 0x20) + 0x20
  Position.Y: (0x0089CDA8 + 0x20) + 0x24
  Position.Z: (0x0089CDA8 + 0x20) + 0x28
  Velocity.X: (0x0089CDA8 + 0x20) + 0x70
  Velocity.Y: (0x0089CDA8 + 0x20) + 0x74
  Velocity.Z: (0x0089CDA8 + 0x20) + 0x78
  */
  </code></pre>
  
  <p>then I realised I could make clever use of the structs..</p>
  
  <p>and I have made this code:</p>
  
  <pre><code>#define AddVar(Type,Name,Address) Type&amp; Name = *reinterpret_cast&lt;Type*&gt;(Address)
  AddVar(unsigned int,PositionBaseAddress,0x0089CDA8);//base address
  
  struct Point { float x, y, z; };
  
  struct VehicleInfo
  {
      Point Pos;
      int unknown[0x11];
      Point Velocity;
  };
  
  void FrameTick(IDirect3DDevice9 * device, HWND wnd)
  {
      DirectXFont::Access(0)-&gt;Print(0.0,0.0,0xFFFFFFFF,"{FFFF0000}N{FF00FF00}F{FF0000FF}S {FFFFFF00}MOD {FF00FFFF}1.0",true);
      if(PositionBaseAddress)
      {
          auto BaseAddr = *(unsigned int*)(PositionBaseAddress + 0x20);
          if(IsBadReadPtr(&amp;BaseAddr,0x04) != 0)
              return;
          auto&amp; info = *(VehicleInfo*)(BaseAddr + 0x20);
          if(IsBadReadPtr(&amp;info,0x04) != 0)
              return;
          DirectXFont::Access(0)-&gt;Print(0.0,45.0,0xFFFFFFFF,string_format("Position: %.2f %.2f %.2f",info.Pos.x,info.Pos.y,info.Pos.z).c_str(),true);
          DirectXFont::Access(0)-&gt;Print(0.0,60.0,0xFFFFFFFF,string_format("Velocity: %.2f %.2f %.2f",info.Velocity.x,info.Velocity.y,info.Velocity.z).c_str(),true);
      }
  }
  </code></pre>