如何删除在C程序这个分段故障 [英] How can i remove this Segmentation fault in C Program

查看:524
本文介绍了如何删除在C程序这个分段故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我想解决堆栈溢出问题在这个code。
这里在这个code我叫函数p所以递归350000次我有段错误
当我取出35万,并把30万比它正常工作
这里分割的故障出现,因为我递归调用的函数P多次打电话或调用递归函数太深了。

here i want to solve stack overflow issue in this code. here in this code i call function p recursively 350000 times so i got segmentation fault when i remove 350000 and put 300000 than it works fine here segmentation fault comes because of i calling recursively call of function p more times or calling a recursive function too deep.

这是不工作,因为我把如果(我!= 350000)
它停在可以在这个范围内30万至327480.我测试10次

this is not working because i take if(i != 350000). its stop at may be in this range 300000 to 327480. i tested 10 times

code:

#include <stdio.h>

void p(char *, int);

int main() 
{
    char *a = "HI";
    int b = 10;
    p(a, b);
    printf("\nComplete");
    return 0;

}
void p(char *a, int b) 
{
    static long int i = 0;

    if (i != 350000) 
    {
        printf("\n%ld \t at Hi hello", i);
        i++;
        p(a, b);
    } else 
    {
        return;
    }
}

当我使用的valgrind工具比valgraind报告这样的错误检查此code

when i check this code using valgrind tool than valgraind reporting error like this

==9236== Stack overflow in thread 1: can't grow stack to 0x7fe801ff8
==9236== 
==9236== Process terminating with default action of signal 11 (SIGSEGV)
==9236==  Access not within mapped region at address 0x7FE801FF8
==9236==    at 0x4EA012E: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1276)
==9236==  If you believe this happened as a result of a stack
==9236==  overflow in your program's main thread (unlikely but
==9236==  possible), you can try to increase the size of the
==9236==  main thread stack using the --main-stacksize= flag.
==9236==  The main thread stack size used in this run was 8388608.
==9236== Stack overflow in thread 1: can't grow stack to 0x7fe801ff0
==9236== 
==9236== Process terminating with default action of signal 11 (SIGSEGV)
==9236==  Access not within mapped region at address 0x7FE801FF0
==9236==    at 0x4A2269F: _vgnU_freeres (vg_preloaded.c:58)
==9236==  If you believe this happened as a result of a stack
==9236==  overflow in your program's main thread (unlikely but
==9236==  possible), you can try to increase the size of the
==9236==  main thread stack using the --main-stacksize= flag.
==9236==  The main thread stack size used in this run was 8388608.

请帮我家伙我真的想这个问题的解决方案。 ,我不能删除递归从我的code函数调用。

Please help me guys i really want this issue solution. and i cant remove recursively call of function from my code.

推荐答案

栈是的的无限资源。递归非常适用于这些相对快速地(一个排序数组的二进制印章,二进制或者多路树遍历等等)。

Stacks are not an unlimited resource. Recursion lends itself well to algorithms which reduce their search space relatively quickly (binary chop of a sorted array, binary or multi-way tree traversal and so on).

如果你发现自己需要递归级别的算法350的的时候,你真的应该重新考虑使用递归的解决方案。

If you find yourself with an algorithm that requires recursion to the level of three hundred and fifty thousand times, you really should reconsider using a recursive solution.

例如,是这样的:

def addUnsigned (a, b):
    if a == 0:
        return b
    return addUnsigned (a-1, b+1)

是的的一个很好的匹配递归。

is not a good match for recursion.

如果您的真正的不能删除递归,那么你需要做什么暗示的valgrind,改变堆栈大小。例如,我的系统上的链接编辑器, LD ,有一个 - 叠选项,允许您指定版权所有(和可选,致力于)堆栈大小。

If you really cannot remove the recursion, then you need to do what valgrind suggests, change the stack size. For example, the linkage editor on my system, ld, has a --stack option which allows you to specify the reserved (and,optionally, committed) stack size.

这篇关于如何删除在C程序这个分段故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆