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.
CodePudding user response:
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.
CodePudding user response:
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.