plugins/languages/c/skills/memory/SKILL.md
C memory management conventions: safe malloc/calloc/realloc patterns, free + NULL, alignment (aligned_alloc, posix_memalign, _Alignas), memory pools/arenas, RAII-like __attribute__((cleanup)), and leak/error detection with Valgrind, AddressSanitizer, MemorySanitizer, LeakSanitizer. Use when allocating memory, debugging leaks, designing cache-friendly layouts, or hardening against UAF / double-free. Triggers on "malloc 检查", "内存泄漏", "valgrind", "asan", "use-after-free", "double free", "内存对齐", "aligned_alloc", "内存池", "arena allocator".
npx skillsauth add lazygophers/ccplugin c-memoryInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
malloc / calloc / realloc 返回值必须检查。realloc 必须用临时指针接住,失败时原指针仍有效。sizeof 用 sizeof(*ptr) 而非 sizeof(Type),避免类型 / 指针不一致。free 后立即将指针置 NULL,杜绝 UAF / double-free。c-error 中 safe_multiply)。aligned_alloc(C11) 或 posix_memalign。malloc 直调。c-embedded)。// malloc
T *p = malloc(n * sizeof(*p));
if (!p) { perror("malloc"); goto cleanup; }
// calloc:零初始化,自动内置溢出检查
T *p = calloc(n, sizeof(*p));
if (!p) { perror("calloc"); goto cleanup; }
// realloc:临时指针模式
T *tmp = realloc(p, new_n * sizeof(*p));
if (!tmp) { perror("realloc"); goto cleanup; }
p = tmp;
// 释放
free(p);
p = NULL;
cleanup 属性)static inline void auto_free(void *pp) { free(*(void **)pp); }
#define AUTO_FREE __attribute__((cleanup(auto_free)))
void demo(void) {
AUTO_FREE char *buf = malloc(256);
if (!buf) return; // buf 作用域结束时自动 free
/* ... */
}
C23 可结合 [[gnu::cleanup(auto_free)]] 语法。
// C11 aligned_alloc:size 必须是 alignment 的整数倍
void *p = aligned_alloc(64, 1024);
// POSIX
void *q = NULL;
if (posix_memalign(&q, 64, 1024) != 0) { perror("posix_memalign"); }
// 缓存行对齐结构体
_Alignas(64) struct CacheLine { int data[16]; };
static_assert(_Alignof(struct CacheLine) == 64, "cache line align");
typedef struct { uint8_t *base; size_t cap, used; } Arena;
Arena *arena_new(size_t cap) {
Arena *a = malloc(sizeof *a);
if (!a) return NULL;
a->base = malloc(cap);
if (!a->base) { free(a); return NULL; }
a->cap = cap; a->used = 0;
return a;
}
void *arena_alloc(Arena *a, size_t n) {
n = (n + 7) & ~(size_t)7; // 8 字节对齐
if (a->used + n > a->cap) return NULL;
void *p = a->base + a->used; a->used += n; return p;
}
void arena_free(Arena *a) { if (a) { free(a->base); free(a); } }
| 工具 | 调用 | 用途 | 兼容性 |
|------|------|------|-------|
| Valgrind memcheck | valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --error-exitcode=1 ./prog | 泄漏、越界、未初始化(10–50× 慢);检测不到栈/全局溢出 | 通用 |
| AddressSanitizer | -fsanitize=address -fno-omit-frame-pointer -g | 堆/栈/全局越界、UAF、double-free | GCC/Clang,与 UBSan 兼容 |
| LeakSanitizer | -fsanitize=leak 或 ASan 自带 | 仅泄漏,开销低 | GCC/Clang |
| MemorySanitizer | clang -fsanitize=memory -fno-omit-frame-pointer | 未初始化读取;需重编译所有依赖 | 仅 Clang,独立运行 |
| mtrack / heaptrack | heaptrack ./prog | 分配画像、火焰图 | Linux |
ASan + UBSan = 日常开发;提交前再跑 Valgrind;怀疑未初始化时用 MSan;与 TSan 不可同跑。
// calloc 内置溢出检查;手写 malloc 时显式校验
if (n > SIZE_MAX / sizeof(T)) return ERR_OVERFLOW;
T *p = malloc(n * sizeof(*p));
// reallocarray (glibc/BSD) 自动检查溢出
T *tmp = reallocarray(p, new_n, sizeof(*p));
if (!tmp) { /* p 仍有效 */ }
free(p); p = NULL; 配对sizeof(*p) 而非 sizeof(Type)aligned_alloc / posix_memalignmalloc/freetools
UI/UX 与布局设计——做界面布局/结构/导航/组件/交互的设计决策。触发:做UI/UX/布局/排版/导航/组件/交互/栅格/响应式/图表选型/字体配对。按媒介路由 HTML/Web、原生 App(iOS/Android/桌面)、CLI、TUI。需后端动态系统不适用;配色/主题/色板走姊妹 skill design-color。
tools
主题与配色设计——做颜色搭配/调色板/主题/品牌色阶/暗模式的设计决策。触发:选配色/调色/主题/色板/品牌色/暗模式/对比度/色盲/UI风格。按媒介路由 HTML/Web(CSS变量)、原生App(平台token)、CLI(ANSI)、TUI(真彩/256/16降级)。保证可访问性(对比度/色盲安全)。需后端动态系统不适用;UI/UX 布局/组件/交互走姊妹 skill design-uiux。
tools
跨任意组件(plugin/skill/agent/command)的验证驱动优化循环纪律 skill。当用户要优化某个已有组件却无明确方向、或要防止改了反而更差(自评乐观偏差 / 多维同改归因失效 / 为凑分加废话膨胀)、或要把一套通用「评分→单变量改→改后验证严格更好才留否则回滚→触顶停」的纪律套到任意组件上时使用。管优化过程本身的纪律(validation gate / ratchet / 独立验证 / 触顶停),不评单组件深度(交 skill-dev),不查插件接线(交 plugin-dev)。仅手动 /optimize-any 触发。
data-ai
两层规则记忆 (基于 .skein/spec)。planning 时 recall 召回相关规则、task finish 后 sediment 沉淀学习 + prune 自动精简过期/重复/断链规则。core 常驻硬规 + recall 按需召回, 经判定门自动写盘 (不逐次问用户)。产出 .skein/spec 下 core/recall 规则文件 + index。另支持空仓 bootstrap 播种规则基线、记忆大面积失效 (大重构/换栈) 时 reconstruct 可逆归档后按项目类型分型重建、maintain 手动体检 (超预算/stale/断链/重复/废弃, --apply 自动修复)、auto-fix (Stop hook 写 .pending-fix 标记 → main 派 skein-specer bg 跑 maintain --apply 全自动修, 断链只报告)。