• Pointers
  • 在getchar处断点,断点后,调试->窗口->反汇编 查看数据

main

#include <iostream>
#include <Windows.h> /*
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;
}

x86打印结果:

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

x64打印结果:

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

c++指针练习的更多相关文章

  1. TODO:Golang指针使用注意事项

    TODO:Golang指针使用注意事项 先来看简单的例子1: 输出: 1 1 例子2: 输出: 1 3 例子1是使用值传递,Add方法不会做任何改变:例子2是使用指针传递,会改变地址,从而改变地址. ...

  2. enote笔记法使用范例(2)——指针(1)智能指针

    要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...

  3. C++虚函数和函数指针一起使用

    C++虚函数和函数指针一起使用,写起来有点麻烦. 下面贴出一份示例代码,可作参考.(需要支持C++11编译) #include <stdio.h> #include <list> ...

  4. C++11 shared_ptr 智能指针 的使用,避免内存泄露

    多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...

  5. c 数组与指针的使用注意事项

    数组变量和指针变量有一点小小的区别 所以把数组指针赋值给指针变量的时候千万要小心 加入把数组赋值给指针变量,指针变量只会包含数组的地址信息 而对数组的长度一无所知 相当于指针丢失了一部分信息,我们把这 ...

  6. Marshal.Copy将指针拷贝给数组

    lpStatuss是一个UNITSTATUS*的指针类型实例,并包含SensorDust字段 //定义一个数组类型 byte[] SensorDust = new byte[30] //将指针类型拷贝 ...

  7. C++智能指针

    引用计数技术及智能指针的简单实现 基础对象类 class Point { public: Point(int xVal = 0, int yVal = 0) : x(xVal), y(yVal) { ...

  8. EC笔记:第三部分:17、使用独立的语句将newed对象放入智能指针

    一般的智能指针都是通过一个普通指针来初始化,所以很容易写出以下的代码: #include <iostream> using namespace std; int func1(){ //返回 ...

  9. 智能指针shared_ptr的用法

    为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...

  10. 智能指针unique_ptr的用法

    unique_ptr是独占型的智能指针,它不允许其他的智能指针共享其内部的指针,不允许通过赋值将一个unique_ptr赋值给另一个unique_ptr,如下面错误用法: std::unique_pt ...

随机推荐

  1. 单机模拟配置Eureka集群

    首先先提醒单机部署的重要点 如果使用一个ip地址(适用于单网卡)每个eureka实例使用不同的域名映射到同一个IP 如果每个eureka实例使用不同的IP(多网卡),要确保这些IP要都表示本地 本文假 ...

  2. 深度学习论文翻译解析(十八):MobileNetV2: Inverted Residuals and Linear Bottlenecks

    论文标题:MobileNetV2: Inverted Residuals and Linear Bottlenecks 论文作者:Mark Sandler Andrew Howard Menglong ...

  3. Spark练习之创建RDD(集合、本地文件),RDD持久化及RDD持久化策略

    Spark练习之创建RDD(集合.本地文件) 一.创建RDD 二.并行化集合创建RDD 2.1 Java并行创建RDD--计算1-10的累加和 2.2 Scala并行创建RDD--计算1-10的累加和 ...

  4. 面向对象编程(封装、封装的意义、封装与扩展性、@property)

    1.封装之如何实现属性的隐藏 封装: __x=1 # 把数据属性隐藏 (如何实现隐藏) 类定义阶段 __开头发生了变形 __x --> _A__x特点: 1.在类外部无法直接:obj.__Att ...

  5. 【从零开始撸一个App】RecyclerView的使用

    目标 前段时间打造了一款简单易用功能全面的图片上传组件,现在就来将上传的图片以图片集的形式展现到App上.出于用户体验考虑,加载新图片采用[无限]滚动模式,Android平台上我们优选Recycler ...

  6. UI中的事件系统EventSystem

    一.EventSystem简介 用于处理事件的分发和相应的系统,创建画布的同时会创建事件系统 二.UGUI实现事件系统的3种方式 1.使用组件eventTrigger(不推荐),拖动赋值 2.代码添加 ...

  7. Python3内置类型有哪些?

    摘要:Python3目前已经成为主流,和版本2天壤之别,关于Python3的内置类型你了解吗? 本文将专注于解释器支持的内置类型,基于版本3.9.1进行讲解. 内置的主要类型是numerics.seq ...

  8. Python3.9.1中如何使用split()方法?

    本文出自:lunvey,半路出家学编程之Python.split()方法定义于str类中,str类大家都知道是python内置定义的一个字符串类. split()默认两个参数,分别是分隔符和分隔数量, ...

  9. 2019牛客暑期多校训练营(第九场)E.All men are brothers(并查集+排列组合)

    题意:现在有n个集合 每个集合大小为1 现在你可以把集合合并m次 每次会告诉你哪个集合合并 让你输出每次从不同的四个集合里各选出四个的组合方案 思路:我们可以想到用并查集模拟集合的合并 对于方案数 我 ...

  10. CodeForces 893C (并查集板子题)

    刷题刷到自闭,写个博客放松一下 题意:n个人,m对朋友,每寻找一个人传播消息需要花费相应的价钱,朋友之间传播消息不需要花钱,问最小的花费 把是朋友的归到一起,求朋友中花钱最少的,将所有最少的加起来. ...