3

I'm running on Linux version 5.0.0-29-generic, with a maximum stack size of 8192KBytes. Is it always 8192 KBytes?

void main()
{
    char arr[8384000] = {};
    int x = 3;
}

I've run this code, and it only seg-faulted 50% of the time, I wonder why that is.

sergey@ipkt /proj/sergey/experiments (master) $ ./3
Segmentation fault (core dumped)
sergey@ipkt /proj/sergey/experiments (master) $ ./3
Segmentation fault (core dumped)
sergey@ipkt /proj/sergey/experiments (master) $ ./3
sergey@ipkt /proj/sergey/experiments (master) $ ./3
sergey@ipkt /proj/sergey/experiments (master) $ ./3
sergey@ipkt /proj/sergey/experiments (master) $ ./3
Segmentation fault (core dumped)
sergey@ipkt /proj/sergey/experiments (master) $ ./3
Segmentation fault (core dumped)
manish ma
  • 1,706
  • 1
  • 14
  • 19
  • Overflowing the stack is undefined behaviour. So you cannot expect it to seg fault each time or even any other behaviour. – kaylum Mar 26 '20 at 10:10
  • 1
    The stack size can vary a bit due to [Address space layout randomization](https://en.wikipedia.org/wiki/Address_space_layout_randomization). – Weather Vane Mar 26 '20 at 10:10
  • The empty initializer (`{}`) is not valid C. Maybe you're using an extension, or a compiler for some other (C-like) language... – pmg Mar 26 '20 at 10:12
  • It's a compiler option, if that's what you mean. – Mark Setchell Mar 26 '20 at 10:22
  • 1
    It's clear what OP means. The stack is supposed to be such-a-size but it is not always that size. Sometimes the code gets stack overflow, sometimes does not. It has nothing to do with syntax errors. – Weather Vane Mar 26 '20 at 10:24

0 Answers0