c++ポインタ練習
3948 ワード
main
#include
#include
/*
Player : object
Name : string
Health : integer
Coins : integer
Coordinates : object
X : float
Z : float
Y : float
Inventory : array - Array of item objects, having the item and item count.
*/
uintptr_t _Inventory[3] = { 1,2,3 };
struct _Coordinates
{
float x = 4.0;
float y = 2.0;
float z = 3.0;
} coordinates;
struct Player
{
const char* Name = "ab";
uintptr_t Health = 6;
uintptr_t Coins = 3;
/*
// coordinates
// Padding1 playerBaseAddress+4*6
_Coordinates Coordinates = coordinates;
float x = 4.0;
float y = 2.0;
float z = 3.0;
*/
_Coordinates* Coordinates = &coordinates;
// uintptr_t Padding1 = 1;
/*
//
//std::cout << "arrar[0]: " << *(uintptr_t*)(playerBaseAddress + sizeof(uintptr_t) * 4) << std::endl;
//std::cout << "arrar[1]: " << *(uintptr_t*)(playerBaseAddress + sizeof(uintptr_t) * 5) << std::endl;
//std::cout << "arrar[2]: " << *(uintptr_t*)(playerBaseAddress + sizeof(uintptr_t) * 6) << std::endl;
const int Inventory[3] = { 1,2,3 };
*/
// , &
uintptr_t* Inventory = _Inventory;
} player;
int main()
{
std::cout << "playerBaseAddress: " << &player << std::endl;
uintptr_t playerBaseAddress = (uintptr_t)&player;
// name
// lea stringNameAddress, [playerBaseAddress]
uintptr_t* stringNameAddress = (uintptr_t*)(playerBaseAddress);
//
// mov eax, dowrd ptr [stringNameAddress]
std::cout << "Name: " << std::hex << *(uintptr_t*)(*stringNameAddress) << std::endl;
// get Health
std::cout << "Health: " << *(uintptr_t*)(playerBaseAddress + sizeof(uintptr_t)) << std::endl;
// get Coins
std::cout << "Coins: " << *(uintptr_t*)(playerBaseAddress + sizeof(uintptr_t) * 2) << std::endl;
// Coordinates
uintptr_t coordinatesAddress = *(uintptr_t*)(playerBaseAddress + sizeof(uintptr_t) * 3);
std::cout << "CoordinatesAddress: " << coordinatesAddress << std::endl;
std::cout << "Coordinates->x: " << *(float*)(coordinatesAddress) << std::endl;
std::cout << "Coordinates->y: " << *(float*)(coordinatesAddress + sizeof(float)) << std::endl;
std::cout << "Coordinates->z: " << *(float*)(coordinatesAddress + sizeof(float) * 2) << std::endl;
// Inventory
uintptr_t InventoryAddress = *(uintptr_t*)(playerBaseAddress + sizeof(uintptr_t) * 4);
std::cout << "InventoryAddress: " << InventoryAddress << std::endl;
std::cout << "Inventory[0]: " << *(uintptr_t*)(InventoryAddress) << std::endl;
std::cout << "Inventory[1]: " << *(uintptr_t*)(InventoryAddress + sizeof(uintptr_t)) << std::endl;
std::cout << "Inventory[2]: " << *(uintptr_t*)(InventoryAddress + sizeof(uintptr_t) * 2) << std::endl;
// set
*(uintptr_t*)(playerBaseAddress + sizeof(uintptr_t)) = 4;
*(uintptr_t*)(playerBaseAddress + sizeof(uintptr_t)*2) = 5;
getchar();
return 0;
}
x 86印刷結果:
playerBaseAddress: 0026D05C
Name: 6261
Health: 6
Coins: 3
CoordinatesAddress: 26d050
Coordinates->x: 4
Coordinates->y: 2
Coordinates->z: 3
InventoryAddress: 26d044
Inventory[0]: 1
Inventory[1]: 2
Inventory[2]: 3
x 64印刷結果:
playerBaseAddress: 00007FF7CC8AD028
Name: 6261
Health: 6
Coins: 3
CoordinatesAddress: 7ff7cc8ad018
Coordinates->x: 4
Coordinates->y: 2
Coordinates->z: 3
InventoryAddress: 7ff7cc8ad000
Inventory[0]: 1
Inventory[1]: 2
Inventory[2]: 3