Why the SIGSEGV?

最后都变了- 提交于 2019-12-13 14:27:03

问题


Why is this code throwing up a SIGSEGV:

int main()
{
    unsigned long toshuffle[9765625];

    unsigned long i;

    for (i=0; i< 1000; i++)
        toshuffle[i]= i;

    return 0;
}

Pointers will be appreciated. (No Pun intended :))


回答1:


Use malloc() to get that much memory. You're overflowing the stack.

unsigned long *toshuffle = malloc(9765625 * sizeof(unsigned long));

Of course when you're done with it, you'll need to free() it.

NOTE: In C++, you need to cast the pointer to the correct type.




回答2:


Probably because you can't allocate 9765625 longs on stack (what is this site called again? :)). Use malloc() instead.




回答3:


From the manpage

  • RLIMIT_STACK

The maximum size of the process stack, in bytes. Upon reaching this limit, a SIGSEGV signal is generated. To handle this signal, a process must employ an alternate signal stack (sigaltstack(2)).



来源:https://stackoverflow.com/questions/1629352/why-the-sigsegv

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