0

For the simple code below, whenever I enter the value 200,000 (or any other high value), the program crashes:

    long size;
    printf("Enter the size of the array to be sorted: ");
    scanf("%ld",&size);

Can anybody tell me why? as based on my information, the range of long is much greater than 200,000

TIA

Edit: the prev piece of code is followed by those declarations.

int arrMerge[size];
int arrQuick[size];
int arrSelect[size];
int arrInsert[size];

When I've commented the whole program except those lines (and the ones above) it has crashed. The following terminating message was given:

Process returned -1073741571 (0xC00000FD) execution time : 2.419 s Press any key to continue.

  • 3
    Don't think that code alone will cause a crash. I would guess there is important code or info you are not showing us. Please provide a complete [mre] as well as the exact run log. – kaylum Mar 23 '22 at 23:51
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) – Andreas Wenzel Mar 23 '22 at 23:58
  • @kaylum I've edited the question for more info – Mariam Atef Mar 24 '22 at 00:00

2 Answers2

5

According to this Microsoft documentation, the status code 0xC00000FD stands for STATUS_STACK_OVERFLOW. Your program is failing due to a stack overflow.

By default, a Windows program has a maximum stack size of about 1 megabyte. If you enter the number 200000, then your variable-length arrays will exceed this limit, causing the stack overflow.

You may want to consider allocating the memory for the arrays on the heap instead of the stack, for example using the function malloc. The heap does not have the limitation of only being able to allocate up to a single megabyte. It is able to store much larger amounts of data.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
1

After size is set to 200,000, these definitions:

int arrMerge[size];
int arrQuick[size];
int arrSelect[size];
int arrInsert[size];

create arrays with automatic storage duration and size 800,000 bytes each, assuming int is four bytes, which is common today. That is a total of 3.2 MB.

Automatic storage duration is typically implemented using a stack (except for effects of optimization), and default stack sizes are 8 MiB on macOS, 2 MiB on Linux, and 1 MiB on Microsoft Windows.1 Thus, the arrays overrun the space set for the stack, and the program crashes.

Footnote

1 “MB” stands for megabyte, 1,000,000 bytes. “MiB” stands for Mebibyte, 220 bytes = 1,048,576 bytes.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312