Meaning of a stack overflow in C programming [duplicate]

≯℡__Kan透↙ 提交于 2021-02-16 14:58:25

问题


Here is the code below:

#‎include ‬<stdio.h>

int main()
{
  printf("Stack Overflow");
  main();
}

After compiling and executing this program it will print "Stack Overflow" until its stack overflows. Here, I know what a stack overflow means, that means it will print until memory is full. My question is which memory is it? What is the size of the stack that is overflowing?


回答1:


which memory is it?

It is the stack memory, namely that which the program uses to store information during a function call about where to return to. Each time you call a function, the CPU saves the location of what's currently executing onto the stack, then jumps to the new function. When the function is done, it pops that location off the stack and returns there. This is what makes recursion possible.

What is the size of stack that is overflowing?

The actual size of the stack is completely platform-dependent. Many operating systems tend to limit the size of the stack to a few megabytes, but allow that size to be changed. If the system runs out of virtual memory, this can also limit the stack size. Some (much) older CPUs have a fixed-size stack (the Intel 8008 only has 7 stack entries).



来源:https://stackoverflow.com/questions/25327497/meaning-of-a-stack-overflow-in-c-programming

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!