# 栈溢出

**栈溢出**（stack overflow）是一种运行时故障，发生于程序的调用栈——即记录活动函数调用的那部分内存区域——增长超出分配给它的内存之时。调用栈为每个活动函数调用保存一个栈帧，其中包含返回地址、局部变量和保存的寄存器；当栈超出其边界时，程序通常会崩溃或被操作系统终止。<sup>[1](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Stack.html)</sup><sup> • </sup><sup>[2](https://developer.mozilla.org/en-US/docs/Glossary/Call_stack)</sup> 这一名称也被程序员问答网站 Stack Overflow 所沿用，该网站成立于 2008 年，正是以此错误命名，本文末尾将有所介绍。

| 关键事实 | 详情 |
|---|---|
| 定义 | 调用栈增长超出其允许的内存大小；区别于栈缓冲区溢出，后者是覆盖栈内的数据<sup>[3](https://libstore.ugent.be/fulltxt/RUG01/003/015/188/RUG01-003015188_2021_0001_AC.pdf)</sup> |
| 常见原因 | 意外的无界递归，以及非常大的局部变量，如栈上分配的数组<sup>[4](https://stackoverflow.com/questions/62107481/does-every-function-get-its-own-stack-in-c)</sup><sup> • </sup><sup>[1](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Stack.html)</sup> |
| Linux 行为 | 超出 RLIMIT_STACK（进程栈的最大字节数）会生成 SIGSEGV 信号<sup>[5](https://man7.org/linux/man-pages/man2/getrlimit.2.html)</sup> |
| Java 行为 | 深度递归抛出 java.lang.StackOverflowError，它是 VirtualMachineError 的子类<sup>[6](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/StackOverflowError.html)</sup> |
| 硬件/操作系统防护 | 栈边界处有一个不可访问的保护页，一旦访问即触发故障；Windows 通过编译器插入的 _chkstk 例程逐页增长栈<sup>[7](https://stackoverflow.com/questions/60058873/linux-process-stack-overrun-by-local-variables-stack-guarding)</sup> |
| 编译器防御 | GCC 的 -fstack-clash-protection 逐页分配栈并触碰每一页，使分配无法跳过保护页<sup>[8](https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Instrumentation-Options.html)</sup> |
| 同名网站 | Stack Overflow，由杰夫·阿特伍德和乔尔·斯波尔斯基于 2008 年创立；2021 年被 Prosus 以 18 亿美元收购<sup>[9](https://stackoverflow.blog/2021/12/14/podcast-400-an-oral-history-of-stack-overflow-told-by-its-founding-team)</sup><sup> • </sup><sup>[10](https://www.axios.com/2021/06/02/stack-overflow-coding-service-sold-for-18-billion)</sup> |

## 调用栈及其为何有限

每次函数调用都会分配一块内存，即栈帧，函数在返回之前一直使用它。递归依赖于此：一个调用自身的函数会为每个尚未返回的调用累积一个栈帧，随着调用返回，栈帧按后进先出的顺序出栈。栈的大小是有限的，试图使用超出限额的栈内存的程序会因栈已满而失败。<sup>[1](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Stack.html)</sup>

用栈来管理递归调用的思想归功于 [Edsger W. Dijkstra](https://www.edgepedia.org/zh/edsger-w-dijkstra)（艾兹格·W·迪杰斯特拉），这位荷兰计算机科学家以在程序结构和并发方面的研究著称。他的论文《递归编程》（Recursive Programming），发表于 *Numerische Mathematik* 第 2 卷，第 312–318 页（1960 年），描述了使用栈，使子程序可以在早先的激活尚未完成时被重新进入，从而消除了固定为每个子程序分配工作存储对程序结构所造成的限制。<sup>[11](https://www.microsoft.com/en-us/research/wp-content/uploads/2023/03/Lampson-PCS-from-book.pdf)</sup>

## 原因

**无界递归。** 在实践中，栈溢出错误的首要原因是意外的无限递归，即调用链反复重新进入相同的函数，而没有停止递归的基准情形。<sup>[4](https://stackoverflow.com/questions/62107481/does-every-function-get-its-own-stack-in-c)</sup> 在 Java 中，每次方法调用都会在线程的栈上分配一个新帧，递归调用过深会导致 StackOverflowError。<sup>[6](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/StackOverflowError.html)</sup>

**大型局部分配。** 单个函数也可以通过声明非常大的局部对象来耗尽栈，例如大小接近栈限制的数组。微软的代码分析警告 C6262 会标记栈使用超过阈值的函数，用户模式下默认阈值为 16 KB，内核模式下为 1 KB，并指出未能提交栈的一页会导致栈溢出异常；内核模式代码面临 12 KB 的栈大小限制，且无法增加。<sup>[12](https://learn.microsoft.com/en-us/cpp/code-quality/c6262?view=msvc-170)</sup>

## 系统如何检测

操作系统在栈的末端放置一个不可访问的内存页，即保护页。在 Linux 上，主线程的栈自动增长：当程序触碰保护页时，内核逐页增长栈，直至栈大小限制，超出该限制的访问会触发故障。在 Windows 上，当函数的局部变量超过一页时，编译器会插入一个 _chkstk 例程；它会触碰中间的页，使栈逐页增长，如果栈无法继续增长，就会发生无效页故障。<sup>[7](https://stackoverflow.com/questions/60058873/linux-process-stack-overrun-by-local-variables-stack-guarding)</sup> 在 GNU/Linux 上，栈溢出通常表现为 SIGSEGV 信号，即所谓的段错误。<sup>[1](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Stack.html)</sup>

限制本身是可配置的。Linux 的 RLIMIT_STACK 资源限制可通过 shell 的 ulimit 命令设置，指定进程栈的最大字节数；自 Linux 2.6.23 起，它还限制了用于命令行参数和环境变量的空间。<sup>[5](https://man7.org/linux/man-pages/man2/getrlimit.2.html)</sup> 线程栈的行为与主栈不同：在 Linux 上，除初始线程外的线程栈必须预先完整分配，因为自动增长机制仅对主线程有效。<sup>[7](https://stackoverflow.com/questions/60058873/linux-process-stack-overrun-by-local-variables-stack-guarding)</sup>

## 栈冲突与编译器防御

保护页能阻止增长到其中的栈，但无法阻止大到足以完全跳过保护页并落入另一内存区域的分配。这种攻击称为栈冲突（stack clashing）。GCC 的 -fstack-clash-protection 选项可应对此问题：编译器一次只分配一页栈空间，并在分配后立即访问每一页，因此分配无法在不触发故障的情况下越过操作系统的保护页。<sup>[8](https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Instrumentation-Options.html)</sup><sup> • </sup><sup>[3](https://libstore.ugent.be/fulltxt/RUG01/003/015/188/RUG01-003015188_2021_0001_AC.pdf)</sup>

另一个选项 -fstack-protector 及其更强的变体针对的是缓冲区越界而非栈溢出：它为含有易受攻击对象的函数添加一个保护变量，并在函数退出时检查，以保护返回地址不被局部数组溢出覆盖。这两种保护针对不同的故障模式，常一起使用。<sup>[8](https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Instrumentation-Options.html)</sup><sup> • </sup><sup>[7](https://stackoverflow.com/questions/60058873/linux-process-stack-overrun-by-local-variables-stack-guarding)</sup>

## Stack Overflow 网站

**Stack Overflow** 是一个面向程序员的问答网站，由 Jeff Atwood（[杰夫·阿特伍德](https://www.edgepedia.org/zh/jeff-atwood)）和 Joel Spolsky（乔尔·斯波尔斯基）于 2008 年创立，杰夫·达尔加斯和贾罗德·迪克森不久后加入；创始团队在当年开始的每周播客通话中讨论了这个产品。<sup>[9](https://stackoverflow.blog/2021/12/14/podcast-400-an-oral-history-of-stack-overflow-told-by-its-founding-team)</sup> 第一个问题于 2008 年 7 月 31 日 21:42 提出，网站于 2008 年 9 月 16 日向公众开放，第 100 万个问题于 2010 年 10 月 12 日提出，当时平均每天有 1,245 个问题。<sup>[13](https://stackoverflow.blog/2010/10/13/millionth-stack-overflow-question/)</sup> 它基于 Microsoft ASP.NET、C#、SQL Server 2008 和 jQuery 构建。<sup>[14](https://stackoverflow.blog/2008/09/21/what-was-stack-overflow-built-with/)</sup> 到 2012 年，其所属的 [Stack Exchange](https://www.edgepedia.org/zh/stack-exchange) 网络报告每月访客超过 3000 万。<sup>[15](https://stackoverflow.blog/2012/02/07/farewell/)</sup>

该网站的社区模式以投票为核心：赞成票将好的问题和答案提升到顶部，反对票表示内容不佳，累积的声望可解锁审核编辑和关闭问题等权限。<sup>[16](https://stackoverflow.com/help/why-vote)</sup><sup> • </sup><sup>[17](https://stackoverflow.com/help/whats-reputation)</sup>

**增长与衰退。** 在巅峰时期，公司吸引了约 5000 万月活跃用户，每月发布约 15 万个问题，2010 年代中期公开网站每月查询量超过 25 万次。<sup>[18](https://www.itpro.com/business/leadership/q-and-a-stack-overflow-ceo-prashanth-chandrasekar)</sup><sup> • </sup><sup>[19](https://www.itpro.com/software/development/is-there-a-future-for-stack-overflow-in-software-engineering)</sup> 荷兰投资公司 Prosus 于 2021 年以 18 亿美元收购了 Stack Overflow。<sup>[10](https://www.axios.com/2021/06/02/stack-overflow-coding-service-sold-for-18-billion)</sup> 问题量自 2019 年以来一直在下降，而在 2022 年底 ChatGPT 公开亮相后下降加速；到 2026 年 1 月，该网站当月仅记录到 2,640 次查询。<sup>[18](https://www.itpro.com/business/leadership/q-and-a-stack-overflow-ceo-prashanth-chandrasekar)</sup><sup> • </sup><sup>[19](https://www.itpro.com/software/development/is-there-a-future-for-stack-overflow-in-software-engineering)</sup> [奥克兰大学](https://www.edgepedia.org/zh/university-of-auckland)的研究发现，自 ChatGPT 出现以来，每月问题量下降了近 76%，并报告最高技能的贡献者正在离开该网站。<sup>[20](https://www.techradar.com/pro/quality-decays-exponentially-following-ai-arrival-research-shows-experts-and-contributors-leaving-online-communities-amidst-silent-knowledge-reset)</sup>

公司自己的开发者调查从用户侧追踪了同样的转变：报告使用 AI 工具的开发者从 2023 年的 44% 增长到 2024 年的 62% 和 2025 年的 79%，其中近一半每天使用 AI 工具。<sup>[21](https://stackoverflow.blog/2026/09/30/getting-ready-for-2026-results-a-look-back-on-developer-survey-findings/)</sup> 首席执行官 Prashanth Chandrasekar（普拉尚特·钱德拉塞卡）认为，业务范围比其公开问答网站更广，并指出 Stack Internal 企业产品以及与 AI 开发者的数据合作关系。<sup>[19](https://www.itpro.com/software/development/is-there-a-future-for-stack-overflow-in-software-engineering)</sup>

## References

1. [Stack, GNU C Language Manual](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Stack.html)
2. [Call stack, MDN Web Docs Glossary](https://developer.mozilla.org/en-US/docs/Glossary/Call_stack)
3. [Protecting Against Stack Overflow and Stack Clashing, Ghent University thesis (2021)](https://libstore.ugent.be/fulltxt/RUG01/003/015/188/RUG01-003015188_2021_0001_AC.pdf)
4. [Does every function get its own stack in C?, Stack Overflow](https://stackoverflow.com/questions/62107481/does-every-function-get-its-own-stack-in-c)
5. [getrlimit(2), Linux manual page](https://man7.org/linux/man-pages/man2/getrlimit.2.html)
6. [StackOverflowError, Java SE 16 API documentation, Oracle](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/StackOverflowError.html)
7. [Linux process stack overrun by local variables (stack guarding), Stack Overflow, answer by Peter Cordes](https://stackoverflow.com/questions/60058873/linux-process-stack-overrun-by-local-variables-stack-guarding)
8. [Instrumentation Options, Using the GNU Compiler Collection (GCC 13.1.0)](https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Instrumentation-Options.html)
9. [Podcast 400: An oral history of Stack Overflow, told by its founding team, Stack Overflow Blog (2021)](https://stackoverflow.blog/2021/12/14/podcast-400-an-oral-history-of-stack-overflow-told-by-its-founding-team)
10. [Stack Overflow coding service sold for $1.8 billion, Axios (2021)](https://www.axios.com/2021/06/02/stack-overflow-coding-service-sold-for-18-billion)
11. [Edsger Wybe Dijkstra: His Life, Work, and Legacy, Microsoft Research, including "Recursive Programming", Numerische Mathematik 2, 312–318 (1960)](https://www.microsoft.com/en-us/research/wp-content/uploads/2023/03/Lampson-PCS-from-book.pdf)
12. [Warning C6262, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/code-quality/c6262?view=msvc-170)
13. [Millionth Stack Overflow Question, Stack Overflow Blog (2010)](https://stackoverflow.blog/2010/10/13/millionth-stack-overflow-question/)
14. [What Was Stack Overflow Built With?, Stack Overflow Blog (2008)](https://stackoverflow.blog/2008/09/21/what-was-stack-overflow-built-with/)
15. [Farewell, Stack Overflow Blog (2012)](https://stackoverflow.blog/2012/02/07/farewell/)
16. [Why is voting important?, Stack Overflow Help Center](https://stackoverflow.com/help/why-vote)
17. [What is reputation? How do I earn (and lose) it?, Stack Overflow Help Center](https://stackoverflow.com/help/whats-reputation)
18. [Stack Overflow CEO Prashanth Chandrasekar on embracing AI, ITPro (2025)](https://www.itpro.com/business/leadership/q-and-a-stack-overflow-ceo-prashanth-chandrasekar)
19. ["If it would go away tomorrow, I wouldn't even notice it": is there a future for Stack Overflow in software engineering?, ITPro (2026)](https://www.itpro.com/software/development/is-there-a-future-for-stack-overflow-in-software-engineering)
20. ['Quality decays exponentially following AI arrival': Research shows experts and contributors leaving online communities, TechRadar (2026)](https://www.techradar.com/pro/quality-decays-exponentially-following-ai-arrival-research-shows-experts-and-contributors-leaving-online-communities-amidst-silent-knowledge-reset)
21. [Getting ready for 2026 results: A look back on Developer Survey findings, Stack Overflow Blog (2026)](https://stackoverflow.blog/2026/09/30/getting-ready-for-2026-results-a-look-back-on-developer-survey-findings/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 19, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Sep 19, 2026; Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/stack-overflow-epf1hxmacj

English original: https://www.edgepedia.org/stack-overflow-epf1hxmacj
