c++指针练习
- 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++指针练习的更多相关文章
- TODO:Golang指针使用注意事项
TODO:Golang指针使用注意事项 先来看简单的例子1: 输出: 1 1 例子2: 输出: 1 3 例子1是使用值传递,Add方法不会做任何改变:例子2是使用指针传递,会改变地址,从而改变地址. ...
- enote笔记法使用范例(2)——指针(1)智能指针
要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...
- C++虚函数和函数指针一起使用
C++虚函数和函数指针一起使用,写起来有点麻烦. 下面贴出一份示例代码,可作参考.(需要支持C++11编译) #include <stdio.h> #include <list> ...
- C++11 shared_ptr 智能指针 的使用,避免内存泄露
多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...
- c 数组与指针的使用注意事项
数组变量和指针变量有一点小小的区别 所以把数组指针赋值给指针变量的时候千万要小心 加入把数组赋值给指针变量,指针变量只会包含数组的地址信息 而对数组的长度一无所知 相当于指针丢失了一部分信息,我们把这 ...
- Marshal.Copy将指针拷贝给数组
lpStatuss是一个UNITSTATUS*的指针类型实例,并包含SensorDust字段 //定义一个数组类型 byte[] SensorDust = new byte[30] //将指针类型拷贝 ...
- C++智能指针
引用计数技术及智能指针的简单实现 基础对象类 class Point { public: Point(int xVal = 0, int yVal = 0) : x(xVal), y(yVal) { ...
- EC笔记:第三部分:17、使用独立的语句将newed对象放入智能指针
一般的智能指针都是通过一个普通指针来初始化,所以很容易写出以下的代码: #include <iostream> using namespace std; int func1(){ //返回 ...
- 智能指针shared_ptr的用法
为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...
- 智能指针unique_ptr的用法
unique_ptr是独占型的智能指针,它不允许其他的智能指针共享其内部的指针,不允许通过赋值将一个unique_ptr赋值给另一个unique_ptr,如下面错误用法: std::unique_pt ...
随机推荐
- Java 类的加载与初始化
本文结构: 1.先看几道题 2.类的加载于初始化 (1)类的加载 (2)类的初始化 (a)会发生类的初始化的情况 (b)不会发生类的初始化的情况 首先看几道题. 解析可在看完讲解后再看 Demo1 p ...
- 「THP3考前信心赛」题解
目录 写在前面 A 未来宇宙 B 空海澄澈 C 旧约酒馆 算法一 算法二 D 博物之志 算法一 算法二 算法三 写在前面 比赛地址:THP3 考前信心赛. 感谢原出题人的贡献:第一题 CF1422C, ...
- Kepware软件基本操作及使用Java Utgard实现OPC通信
一.环境搭建(基于win10 64位专业版) 1.Kepware 的下载.安装及使用 https://www.cnblogs.com/ioufev/p/9366877.html 2.重要:OPC 和 ...
- log4j 动态配置,重启项目配置失效问题
公司项目升级之后,成功去掉了log4j.properties配置文件,实现页面动态配置日志级别. 很经典的两个配置,但是最终还是随着时代的进步而被优化,最终弄成了可配置项 但是随之问题就来了,当我启动 ...
- Linux数据库的导入导出
Linux数据库的导入导出 1.导入数据库 mysql -u username -p test < /home/data/test.sql 说明:username是数据库用户名,test为目标数 ...
- 一文读懂云上DevOps能力体系
简介: 阿里云ECS自动化运维套件架构师,深度拆解云上运维能力体系建设:自动化运维等级金字塔.自动化运维的进阶模式.DevOps的基础核心.云上标准化部署三大能力-- 序言 云计算行业已经有十多年的发 ...
- 弱网测试之Fidder
是用Fidder可以模拟若罔测试. 1.Fiider设置 fiddler中选中Rules->Cutomize Rules,在文件中搜索关键字:m_SimulateModem: 修改m_Simul ...
- Jenkins(3)拉取git仓库代码,执行python自动化脚本
前言 python自动化的脚本开发完成后需提交到git代码仓库,接下来就是用Jenkins拉取代码去构建自动化代码了 新建项目 打开Jenkins新建一个自由风格的项目 源码管理 Repository ...
- B 等差素数列
B 等差素数列:2,3,5,7,11,13,....是素数序列.类似:7,37,67,97,127,157 这样完全由素数组成的等差数列,叫等差素数数列.上边的数列公差为30,长度为6.2004年,格 ...
- Codeforces Round #652 (Div. 2) E. DeadLee 贪心
题意: 派会上有n种食物,每种食物有wi份.有m个朋友,每一个朋友有两种他喜欢吃的食物xi,yi.你需要判断他的朋友是否都能吃到食物.如果都能吃到食物,那么要输出朋友来的顺序,不能的话输出" ...