c - 如何在不推送新堆栈帧的情况下溢出堆栈?

标签 c segmentation-fault stack-overflow

导致堆栈溢出并获得段错误 的一种明显方法是递归地将堆栈帧推到彼此之上,直到它 booms。我想知道是否会在不推送新堆栈帧的情况下发生堆栈溢出。

创建足够大的阵列也可以根据经验来实现,但还有其他可能的情况吗?

最佳答案

C99 使用可调整大小的数组,您可以使用它并将其大小不断调整为更大的数组。然而,这个可调整大小的数组是使用 alloca 实现的。以下是 UNIX env 中的示例代码:

#include <stdio.h>
#include <alloca.h>
#include <stdlib.h>
#include <stdbool.h>

int
main()
{
    while (true)
    {
        void *p = alloca(32UL);
        printf("new memory allocated at %p \n", p);
    }
    exit(EXIT_SUCCESS);
}

你的输出看起来像这样

new memory allocated at 0xbf800a60 
new memory allocated at 0xbf800a30 
new memory allocated at 0xbf800a00 
new memory allocated at 0xbf8009d0 
new memory allocated at 0xbf8009a0 
[1]    3977 segmentation fault  ./a.out

alloca 属于malloc 函数家族,只是它通过调整堆栈指针在堆栈上分配内存。

关于c - 如何在不推送新堆栈帧的情况下溢出堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10102949/

相关文章:

c - 多线程编程: Under what situation does the variable 'iget' is equal to the variable 'iput' ?

c - 如何在c中的curl post请求中设置cookie

c++ - 为什么将单个枚举封装在一个结构中?

c - c 中 main 的程序参数 - ./dev --print

java - 我抛出的异常遇到 StackOverflowError

Clojure - 迭代惰性集合时出现 StackOverflowError

c - GTK+ 将 gdk_draw_pixbuf 替换为 Cairo

c - if 语句导致段错误

构建线段树时的 C++ 段错误

.net - 为什么使用 NUnit/TestDriven.Net2.0 测试会崩溃?