[英]Why does stack overflow occur if we do not declare our buffer as a global variable in C?
Just read somewhere that, 只是在那个地方读
when large buffers are used they must be made global variables otherwise stack overflow occurs. 当使用大缓冲区时,必须将它们设为全局变量,否则会发生堆栈溢出。
But i am unable to get why it would happen? 但是我不知道为什么会发生?
(This is for C language compilers Turbo C on windows). (这适用于Windows上的C语言编译器Turbo C)。
全局变量不在堆栈上分配-它们位于可执行文件的读写部分,因此不会导致堆栈溢出。
Typically, the executable file header, and OS loader, place a lower limit on the maximum stack size than they do on the global/static data. 通常,可执行文件头和OS加载程序对最大堆栈大小的限制比对全局/静态数据的限制低。
Your linker is responsible for assembling the executable header and may be instructed to set a greater, (or smaller), maximum stack size for the executable image, so you could instruct the linker to raise that limit to accommodate your big buffer. 链接程序负责组装可执行文件头,并可能会指示为可执行文件映像设置更大(或更小)的最大堆栈大小,因此您可以指示链接程序提高该限制以容纳较大的缓冲区。
If you need a large buffer and don't want to increase the maximum stack, you could dynamically-allocate the buffer space at runtime. 如果您需要一个较大的缓冲区并且不想增加最大堆栈,则可以在运行时动态分配缓冲区空间。
The last option you should consider is using statics/globals. 您应该考虑的最后一个选项是使用statics / globals。 Such code/data is not reeentrant and so not thread-safe:( 此类代码/数据不是新的,因此也不是线程安全的:(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.