知序
返回
保存草稿
发布
H1
H2
H3
1
2
3
Excalidraw 图
AI 写作
AI 优化
目录
输入内容后显示目录
Markdown
# 附 1 - 实验代码:target.c 靶子程序:构造 4 级指针链,并打印推算所需的信息。对应正文第 01、03、04、05、06、07、08 课。 ```c // target.c —— 学习靶子程序:构造一条 4 级指针链,并打印推算链所需的全部信息 // // 编译(任选其一,位数要与 reader 一致): // gcc target.c -o target.exe (MinGW / MSYS2) // cl target.c (MSVC) // gcc target.c -o target (Linux) // // 运行:直接执行,程序每 2 秒打印一次,自己会不断变化血量以便验证读取结果。 // // 目的:观察“静态偏移稳定、动态地址漂移”这一现象,理解锚点为什么必须落在静态区。 #include <stdio.h> #include <stdint.h> #include <stddef.h> #include <string.h> #include <stdlib.h> #ifdef _WIN32 #include <windows.h> #define TICK() Sleep(2000) static uintptr_t module_base(void) { return (uintptr_t)GetModuleHandleA(NULL); } #else #include <unistd.h> #define TICK() sleep(2) // Linux 下模块基址请用 /proc/<pid>/maps 查看(见 README) static uintptr_t module_base(void) { return 0; } #endif typedef struct { // 第 4 级:叶子字段 int32_t hp; int32_t mp; float x, y; char name[16]; } Attr; typedef struct { // 第 3 级:实体 uint32_t id; Attr attr; // 内嵌结构:偏移在编译期就固定 } Entity; typedef struct { // 第 2 级:场景 uint32_t count; Entity* entities; // 指向堆数组 } Scene; typedef struct { // 第 1 级:管理器 uint32_t version; Scene* scene; } SceneManager; // ★ 锚点:静态存储期变量,落在 .data 段 → “模块基址 + 固定偏移”即可定位 static SceneManager* g_manager = NULL; int main(void) { Entity* entities = (Entity*)calloc(2, sizeof(Entity)); Scene scene; SceneManager mgr; if (entities == NULL) { fprintf(stderr, "calloc failed\n"); return 1; } entities[0].id = 1001; entities[0].attr.hp = 100; strcpy(entities[0].attr.name, "player"); entities[1].id = 2001; entities[1].attr.hp = 500; strcpy(entities[1].attr.name, "monster"); scene.count = 2; scene.entities = entities; mgr.version = 1; mgr.scene = &scene; g_manager = &mgr; uintptr_t base = module_base(); printf("=== target 自检信息(供练习推算)===\n"); printf("module base = 0x%llX\n", (unsigned long long)base); printf("&g_manager (锚点地址) = 0x%llX\n", (unsigned long long)(uintptr_t)&g_manager); if (base) { printf("锚点静态偏移 (相对模块) = 0x%llX <-- reader 要用的第一个值\n", (unsigned long long)((uintptr_t)&g_manager - base)); } else { printf("锚点静态偏移 : Linux 请在 /proc/<pid>/maps 中定位本程序段后自行相减\n"); } printf("堆地址 (每次运行都变) = 0x%llX\n", (unsigned long long)(uintptr_t)entities); printf("\n--- 各级字段偏移 (offsetof) ---\n"); printf("SceneManager.scene = 0x%zX\n", offsetof(SceneManager, scene)); printf("Scene.entities = 0x%zX\n", offsetof(Scene, entities)); printf("sizeof(Entity) = 0x%zX <-- 数组步长\n", sizeof(Entity)); printf("Entity.attr = 0x%zX\n", offsetof(Entity, attr)); printf("Attr.hp = 0x%zX\n", offsetof(Attr, hp)); printf("\n链: [base + 锚点偏移] -> +(scene) -> +(entities) -> +i*sizeof(Entity) -> +(attr) -> +(hp)\n\n"); for (int i = 0; ; ++i) { entities[0].attr.hp = 100 + i; // 让值动起来,便于验证读的是活数据 printf("[tick %d] player.hp=%-5d monster.hp=%-5d entities=0x%llX\n", i, entities[0].attr.hp, entities[1].attr.hp, (unsigned long long)(uintptr_t)entities); fflush(stdout); TICK(); } return 0; } ```
预览
发布文章
添加摘要让读者更快了解文章内容
取消
确认发布
AI 写作助手
输入一个主题,AI 将为你生成一篇 Markdown 博客文章
AI 正在写作中...
取消
开始生成
AI 优化助手
描述你的修改需求,AI 将在原文基础上进行补充或微调
AI 正在优化中...
取消
开始优化
画布
从已有图里选
✕
图名
保存后生成 /images/excalidraw/<图名>.svg(只能用字母、数字、短横线、下划线、点)
取消
保存并插入