Segmentation fault: Stack allocation in a C program in Ubuntu when bufffer>4M

前端 未结 2 459
借酒劲吻你
借酒劲吻你 2020-12-21 13:03

Here\'s a small program to a college\'s task:

#include 

#ifndef BUFFERSIZE
#define BUFFERSIZE 1
#endif

main()
{
    char buffer[BUFFERSIZE]         


        
2条回答
  •  清歌不尽
    2020-12-21 13:19

    The stack size is quite often limited in Linux. The command ulimit -s will give the current value, in Kbytes. You can change the default in (usually) the file /etc/security/limits.conf. You can also, depending on privileges, change it on a per-process basis via the code:

    #include 
    // ...
    struct rlimit x;
    if (getrlimit(RLIMIT_STACK, &x) < 0)
        perror("getrlimit");
    x.rlim_cur = RLIM_INFINITY;
    if (setrlimit(RLIMIT_STACK, &x) < 0)
        perror("setrlimit");
    

提交回复
热议问题