Skip to content
Skills Library
Programming · Learning & explanation

c-memory-errors

/c-memory-errors

C语言内存/数组/指针错误的知识参考技能,涵盖数组越界(栈/堆/全局/字符串)、野指针、悬垂指针、空指针解引用、指针运算越界、malloc/free配套错误(double free/内存泄漏/分配大小算错)、sizeof数组退化、有符号整数溢出等未定义行为,以及错误现象对照、检测工具(ASAN/Valgrind)和安全编码守则。适用于嵌入式、单片机、硬件方向的C语言开发者和求职者。当用户询问"C语言数组越界/内存错误/野指针/段错误/堆破坏/未定义行为"的总结、排查、面试考点整理,或要求"把C内存错误总结做成可复制文档/练习/背诵版"时使用。

Repository
simpleide ↗
Licence
GPL-3.0
Last repository change
31.10.2026

Assessment and security

95/ 100Class: good
Format20 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
2
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 31.10.2026, without references to retired models or APIs.

Licence

GPL-3.0, otwarta.

What it does and when to use it

C语言内存/数组/指针错误的知识参考技能,涵盖数组越界(栈/堆/全局/字符串)、野指针、悬垂指针、空指针解引用、指针运算越界、malloc/free配套错误(double free/内存泄漏/分配大小算错)、sizeof数组退化、有符号整数溢出等未定义行为,以及错误现象对照、检测工具(ASAN/Valgrind)和安全编码守则。适用于嵌入式、单片机、硬件方向的C语言开发者和求职者。当用户询问"C语言数组越界/内存错误/野指针/段错误/堆破坏/未定义行为"的总结、排查、面试考点整理,或要求"把C内存错误总结做成可复制文档/练习/背诵版"时使用。

Triggers from the description

C语言数组越界/内存错误/野指针/段错误/堆破坏/未定义行为把C内存错误总结做成可复制文档/练习/背诵版
Original author description

C语言内存/数组/指针错误的知识参考技能,涵盖数组越界(栈/堆/全局/字符串)、野指针、悬垂指针、空指针解引用、指针运算越界、malloc/free配套错误(double free/内存泄漏/分配大小算错)、sizeof数组退化、有符号整数溢出等未定义行为,以及错误现象对照、检测工具(ASAN/Valgrind)和安全编码守则。适用于嵌入式、单片机、硬件方向的C语言开发者和求职者。当用户询问"C语言数组越界/内存错误/野指针/段错误/堆破坏/未定义行为"的总结、排查、面试考点整理,或要求"把C内存错误总结做成可复制文档/练习/背诵版"时使用。

How it works

  1. Overview
  2. 回答工作流(Workflow)
  3. 输出格式要求
  4. 参考资源
  5. 常用示例速查(无需读 reference 即可回答的原子事实)
Original SKILL.md headings · 76 lines, 192 words
SKILL.md preview
---
name: c-memory-errors
description: C语言内存/数组/指针错误的知识参考技能,涵盖数组越界(栈/堆/全局/字符串)、野指针、悬垂指针、空指针解引用、指针运算越界、malloc/free配套错误(double free/内存泄漏/分配大小算错)、sizeof数组退化、有符号整数溢出等未定义行为,以及错误现象对照、检测工具(ASAN/Valgrind)和安全编码守则。适用于嵌入式、单片机、硬件方向的C语言开发者和求职者。当用户询问"C语言数组越界/内存错误/野指针/段错误/堆破坏/未定义行为"的总结、排查、面试考点整理,或要求"把C内存错误总结做成可复制文档/练习/背诵版"时使用。
---

# C 内存与数组错误参考

## Overview

为嵌入式/硬件 C 开发提供内存类错误的完整知识库与标准回答套路。核心前提:**C 语言不做数组边界检查**,越界、野指针、堆破坏均属于未定义行为(UB),不必然崩溃,表现为"偶现 bug、变量被篡改、free 时才炸"等难以复现的现象。

## 回答工作流(Workflow)

无论用户要"总结""排查""面试考点"还是"做题",统一按此流程组织答案:

1. **判定错误类别** —— 从用户描述的现象/代码定位到具体类别(见 reference 的《错误现象对照表》)。
2. **讲清根因** —— 一句话点明这是哪种 UB、C 为什么不拦它(无边界检查、指针退化、内存布局等)。
3. **给出最小复现代码** —— 附可运行的错误示例 + 安全写法(成对出现)。
4. **说明后果与触发时机** —— 尤其点出"延迟报错":如堆越界常在 free 时才崩。
5. **给出检测/排查手段** —— ASAN、Valgrind、编译警告。
6. **列出安全编码守则** —— 对应的防错规范。
7. **(可选)收尾** —— 按需生成背诵版/Word 文档/练习题。

## 输出格式要求

- 遵循用户偏好:**"问题 + 答案"完整格式**,内容详尽、排版整洁、可直接复制到 Word。
- 分类用标题分节,代码块标注语言,危险/安全写法成对对比。
- 涉及多类错误或对照关系时,优先用**表格**(现象对照、工具对照)。
- 核心结论先说,再展开细节;避免冗余铺垫。

## 参考资源

详细知识库位于 `references/error-catalog.md`(C 内存错误分类目录)。内容超出当前提问范围时按需加载,避免一次加载全部。

- 分类目录(数组越界/指针类/堆类/sizeof/UB 系列)
- 错误现象 → 高度怀疑原因对照表
- 检测工具与启用方式
- 安全编码守则
- 嵌入式(STM32/单片机)场景特别提醒

根据问题定位后,先读该文件对应章节;需要生成面试题或背诵版时,基于该文件的分类与示例自行编排。

## 常用示例速查(无需读 reference 即可回答的原子事实)

**数组越界**
```c
char buf[16];  buf[16] = 'a';   // 栈越界,下标0~15合法
int *p = malloc(4*sizeof(int)); p[4]=1;  // 堆越界,破坏堆管理头,free时才崩
char s[8]; strcpy(s,"123456789");  // strcpy不检查长度
```

**指针类**
```c
int *p;       // 未初始化野指针
int *q=malloc(4); free(q);   // q成为悬垂指针,free后未置NULL
*q=10;        // 悬垂/野指针解引用 = UB
*(a+5);       // 尾后指针 a+5 只能比较不能解引用
a[-1]=1;      // 下标负数向前越界
```

**堆配套错误**
```c
free(p); free(p);  // double free
free(arr);         // arr是栈数组,free非malloc地址 → 崩溃
int *p=malloc(10); // 想存10个int只分10字节,应为10*sizeof(int)
```

**sizeof 数组坑**
```c
void f(char buf[]){ int n=sizeof(buf); }  // buf退化为指针,sizeof==8(64位),非数组长度
char b[16]; sizeof(b);  // 定义作用域内才为数组总字节
```

**其他 UB**
```c
int a=INT_MAX; a=a+1;   // 有符号溢出=UB;unsigned溢出是定义好的模2^n
char *p="hello"; p[0]='H';  // 修改只读字符串字面量→段错误;char p[]="hello"可改
int* test(){ int a[10]; return a; }  // 返回栈地址→悬垂指针
```
Files in this package (2)
  • SKILL.md 4 KB
  • references/error-catalog.md 7 KB

Versions

The database has no other skills named c-memory-errors.

Database snapshot: 13.11.2026 · package version 20ddff5889d2 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100valgrind
    Use when running Valgrind Memcheck for heap errors, leaks, or uninitialised reads on an unmodified binary, or Cachegrind, Callgrind, or Massif profiling. Not for ASan builds: use sanitizers.
  • 90cc-native-memory-safety
    Use when C/C++ leaks, use-after-free, double release, bounds errors, allocator mismatch, FFI ownership or teardown races need diagnosis or repair.
  • 100c-systems
    C patterns for systems code, libraries, and native extensions: module layout, function decomposition, status-enum errors, memory safety, undefined behavior, and performance measurement. Use when writing, reviewing, refactoring, or debugging C, working with malloc lifetimes,…
  • 100valgrind
    Valgrind profiler skill for memory error detection and cache profiling. Use when running Memcheck to find heap corruption, use-after-free, memory leaks, or uninitialised reads; or Cachegrind/Callgrind for cache simulation and function-level profiling. Activates on queries about…
  • 96sanitizer-memory-debugging
    Use when ASan/LSan reports nothing on a real target.
  • 95c-memory-management
    Use when managing memory in C programs with malloc/free, pointers, and avoiding common memory safety pitfalls.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.