简体   繁体   English

如何在运行时检查堆栈溢出

[英]how stack overflow is checked in runtime

I am mainly thinking about Windows. 我主要在考虑Windows。

AFAIK on such platforms there are many stacks, each program, or maybe even each thread has its own stack, and each of such threads can push bytes onto it - AFAIK every of such push should be checked in runtime in case of stack overflow - so it seem it is some cost related to each and every push (something like arrays bounds checking) - how exactly this checking is implemented ? 在此类平台上的AFAIK有很多堆栈,每个程序,甚至每个线程都有自己的堆栈,并且每个这样的线程都可以将字节推入到其上-AFAIK在堆栈溢出的情况下,应在运行时检查每个此类推入-因此似乎每一次推送都涉及一些成本(类似于数组边界检查)-这种检查的实现方式是如何?

On old machines as I remember there was no checking but some fff become 000 so there was no cost of checking, but today on windows platform it seem to me that probably every stack is bound checked - but I do not know how it is implemented. 我记得在旧机器上没有进行检查,但是一些fff变为000,因此没有检查成本,但是今天在Windows平台上,我似乎可能已对每个堆栈都进行了检查-但我不知道它是如何实现的。

I'm not aware of any fully-compiled language on Windows or Linux platforms that does call stack bounds checking by default. 我不知道Windows或Linux平台上的任何完全编译的语言会在默认情况下调用堆栈边界检查。 Thus, overflowing the available stack space leads to a segmentation fault as described in (for instance) the questions Segmentation fault due to recursion and What is the difference between a segmentation fault and a stack overflow? 因此,溢出可用的堆栈空间会导致分段错误,如问题( 由于递归引起的分段错误 )中所述,分段错误和堆栈溢出之间有什么区别? .

The benefit of not doing bounds checking, as observed in the question is that the code runs more quickly. 如问题中所观察到的,不进行边界检查的好处是代码运行更快。 If one wanted to bounds check for some particular reason, one could insert the bounds checks for that specific case. 如果出于某些特定原因想要进行边界检查,则可以针对该特定情况插入边界检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM