当我输入值 200000 作为 long 变量的输入时,C 程序崩溃,为什么?
对于下面的简单代码,每当我输入值 200,000(或任何其他高值)时,程序就会崩溃:
long size;
printf("Enter the size of the array to be sorted: ");
scanf("%ld",&size);
有人能告诉我为什么吗? 根据我的信息,long 的范围远大于 200,000
TIA
编辑: 上一段代码后面是这些声明。
int arrMerge[size];
int arrQuick[size];
int arrSelect[size];
int arrInsert[size];
当我评论除这些行(以及上面的行)之外的整个程序时,它已经崩溃了。给出以下终止消息:
Process returned -1073741571 (0xC00000FD)execution time : 2.419 s 按任意键继续。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 此 Microsoft 文档< /a>,状态码
0xC00000FD
代表STATUS_STACK_OVERFLOW
。您的程序由于堆栈溢出而失败。默认情况下,Windows 程序的最大堆栈大小约为 1 MB。如果您输入数字
200000
,那么您的变长数组将超出此限制,导致堆栈溢出。您可能需要考虑在堆而不是堆栈上为数组分配内存,例如使用函数
malloc
。堆不存在只能分配最多 1 MB 的限制。它能够存储更大量的数据。According to this Microsoft documentation, the status code
0xC00000FD
stands forSTATUS_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.将
size
设置为 200,000 后,这些定义:创建具有自动存储持续时间的数组,每个数组的大小为 800,000 字节,假设
int
为 4 个字节,这在当今很常见。总共 3.2 MB。自动存储持续时间通常使用堆栈来实现(优化效果除外),默认堆栈大小在 macOS 上为 8 MiB,在 Linux 上为 2 MiB,在 Microsoft Windows 上为 1 MiB。1因此,数组超出了堆栈的空间设置,程序崩溃。
脚注
1 “MB”代表兆字节,1,000,000 字节。 “MiB”代表 Mebibyte,220 字节 = 1,048,576 字节。
After
size
is set to 200,000, these definitions: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.