Why SIGSEGV?

Why does this code call 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 :))

+3
source share
3 answers

Use malloc () to get so much memory. You are overflowing the stack.

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

Of course, when you are done with this, you will need to free () it.

NOTE. In C ++, you need to point to a pointer to the correct type.

+15
source

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

+8
source

manpage

  • RLIMIT_STACK

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

+2
source

Source: https://habr.com/ru/post/1721161/


All Articles