-2
#include<stdio.h>

int main(){
  int list[519276];
  printf("Hello");
  return 0;
}

If I change the size of the array "list" from 519276 to anything larger, the message "hello" is no longer displayed in the terminal.

I have also tried it in other programs that made more sense and there the array had to be even smaller for something to be output in the terminal.

Compiling with "gcc" does not cause any problems.

Can anyone explain to me why this is so?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Tim
  • 1
  • 1
    What is a "sunless program"? – mkrieger1 Apr 30 '22 at 20:10
  • The variable `list` is a local variable and thus allocated on the stack. The stack size is limited on most systems. So the program crashes. Without knowing more about your system, it's difficult to tell why you don't get an error when running the program. – Codo Apr 30 '22 at 20:10
  • 2
    BTW: This kind of error is called *Stack Overflow*. – Codo Apr 30 '22 at 20:11
  • You can read more about stack overflows in [this Wikipedia article](https://en.wikipedia.org/wiki/Stack_overflow). – Andreas Wenzel Apr 30 '22 at 20:12
  • Depending on optimization flags, `gcc` may eliminate `list` from the resulting assembly, since it is unused and has no side effects. – Oka Apr 30 '22 at 20:39
  • @mkriger1 I meant pointless not sunless – Tim Apr 30 '22 at 22:00

1 Answers1

1

What you experience is indeed Stack Overflow. but it is happening before your code even starts!

if it is a Console application (which I believe it is),
there are few preparations made by the OS before calling your main method like: Creating 'argv' and 'argc', allocating the console window, redirecting stdin/out and most relevant to the question Allocating STACK for the program

the last step in the preparation code is calling your main which require a significant STACK size which apparently is larger then the default allocation made by the system...

But, it isn't true that it does not report any error! Though it does not emit to console, it should return a Non-Zero return code (and should throw Signal too).

To overcome this: You may either allocate the block on the Heap instead int* list = new int[519276];,
or create a new Thread with sufficient stack size. (P.S. Some OS will let you define the entry point`s stack-size.)

hope this helps.
cheers.

Tomer W
  • 3,395
  • 2
  • 29
  • 44
  • Or even better make it static. – Fredrik Apr 30 '22 at 21:13
  • Not sure static will do it... might, but ain't sure. – Tomer W Apr 30 '22 at 21:18
  • what do you mean static wont do it? – Fredrik Apr 30 '22 at 21:25
  • If you store the variable in my example "list" static it will be stored in the stack, but it is too big for that. Therefore I have to store it in the heap and this is done with mallock. I hope it is right what I say. – Tim Apr 30 '22 at 21:56
  • Static variables are not stored on the stack. – Fredrik Apr 30 '22 at 22:00
  • You are completely right. I just saved the variable as static without allocating the memory itself and that also worked and is easier. I don't know yet what the difference is between a non-static and a static but I'll look into that as well. Thanks! – Tim Apr 30 '22 at 22:17