Windows弹出窗口:X.exe已停止工作(Code :: Blocks)

时间:2013-11-08 21:02:30

标签: c windows codeblocks

提前感谢您看一下。

当我尝试运行我在Code :: Blocks中用C编写的程序时,我收到一条Windows错误消息。有趣的是它编译得很好,如果我降低我正在测试的程序的上限运行良好。

详细说明:

当我尝试运行该程序时,首先我得到一个Windows弹出窗口,上面写着“X.exe已经停止工作.Windows正在检查问题的解决方案”。不久之后,这变为“X.exe已停止工作。一个问题导致程序停止正常工作.Windows将关闭程序并通知您是否有解决方案。(关闭程序)”我单击关闭程序按钮,然后我看到命令提示符显示“进程返回255< 0xFF>执行时间3.940秒按任意键继续”。

我有Windows 8。

我正在使用GNU GCC编译器。

如果我将“upto”更改为100000,程序运行正常。

以下是代码:

/************************************************
* Finds the starting integer under 1000000 that
* produces the longest Collatz sequence, and the
* length of said sequence.
*************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <limits.h>
#define upto 1000000

int main()
{
    long i;
    long long j;
    long long max = LONG_LONG_MAX;
    long length = 0;
    long number = 0;
    long penull = 0;
    long len[upto];
    for (i = 0; i < upto; i++) (len[i] = 0);
    // counts length of Collatz sequence for starting integers from 1 to 999999
    for (i = 1; i < upto; i++)
    {
        j = i;
        while (j != 1)
        {
            assert (j <= (max - 1)/3);
            if (j%2 == 0) (j = j/2);
            else (j = 3*j + 1);
            len[i]++;
            if (j < i)
            {
                len[i] = len[i] + len[j];
                j = 1;
            }
        }
        // stores length of the longest sequence and the starting integer producing it
        if (len[i] > length)
            {
                length = len[i];
                number = i;
            }
        // stores a duplicate length for later comparison
        else if (len[i] == length) (penull = len[i]);
    }
    if (length == penull) (printf("There are at least two!"));
    else printf("%ld produces a Collatz sequence of length %ld", number, length + 1);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

将数组len移到main函数之外。堆栈的大小有时是有限的,局部变量存储在堆栈中。通过将其移到外面,可以使其成为全局变量。

相关问题