0

我目前正在解决 Project Euler 的问题 #60:http ://projecteuler.net/problem=60 (以防万一你想尝试按照我的逻辑)。

问题是,在我构建我的代码(它完成没有错误)然后运行它之后,我从运行它时使用的 IDE 中得到错误代码“线程 1:EXC_Bad_Access(代码 = 1,地址 = 0x7fff55056148)” (我认为 IDE 内置了调试器)。更具体地说,该错误仅发生在我的“组合”功能中。突出显示的行在我的组合函数中被“//”注释行禁用。因此,目前,我的代码将在没有任何错误的情况下运行,因为所有导致错误的行都被禁用为注释。如果您取消注释任何这些行或这些行的任何组合,则代码会运行到上面列出的相同错误代码。

来自实验的个人评论:我发现任何与 ofstream 或我初始化称为 count 的整数有关的任何行都会导致错误。ofstream 有点道理,但即使在禁用与 ofstream 相关的所有代码行之后,整数计数也会突然开始产生错误。

任何帮助将非常感激!我仍然是 C++ 的初学者,(大约两到三周前开始。)

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

/* double x = 2 , y = 2 , b = 3, s = 2; */
/* int z, c = 1, v = 3000; */

int AllPrimes[3000];
/* int AllCombos[2018257871250650][5]; */ // disabled this line for now.

//Used to be within Combinations; Moved here to circumvent "Bad Access" Error

int FindPrimes();
int TestforPrime(double y);
int Combinations();
int WriteArrayToFile(int *ArrayPointer,int ArrayLength, string FileName, char Append);

int main()
{
    cout<<FindPrimes();
    cout<<Combinations();
}

int Combinations() {
    int i1, i2, i3, i4, i5, /* ai */ bi, ci, di, ei;
    int ZeroPointBreaker=0;
//ofstream BufferFlushed ("/Users/Yash/Xcode/Projects/Project Euler Programs/Project Euler Problem 60 (Weird Prime Cocatenate Property Problem)/I:O Files/");
int count=0;
int Buffer[9000000][5];
for (i1=0; i1<2996; i1++) {
    count++;
    // cout<<"Index 1 Iteration: "<<i1<<" || Count Value: "<<count<<"\n";
    bi = i1 + 1;
    for (i2=bi; i2<2997; i2++) {
       count++;
      //  cout<<"Index 2 Iteration: "<<i2<<" || Count Value: "<<count<<"\n";
        ci = i2+ 1;
        for (i3=ci; i3<2998; i3++) {
            count++;
            di = i3 + 1;
            for (i4=di; i4<2999; i4++) {
                count++;
                ei = i4 + 1;
                for (i5=ei; i5<3000; i5++) {
                    count++;
                   // Buffer[count][0]=AllPrimes[i1];
                  //  Buffer[count][1]=AllPrimes[i2];
                  //  Buffer[count][2]=AllPrimes[i3];
                 //   Buffer[count][3]=AllPrimes[i4];
                 //   Buffer[count][4]=AllPrimes[i5];
                }
            }
        }
    //Flush Here
    //   count=0;
        /* for (int i=0; i<9000000; i++) {
            if (Buffer[i][1]==0) {ZeroPointBreaker=i; break;}
        } */
  //      for (int i=0; i<ZeroPointBreaker; i++) {
           // BufferFlushed<<Buffer[i][1]<<','<<Buffer[i][2]<<','<<Buffer[i][3]<<','<<Buffer[i][4]<<','<<Buffer[i][5]<<'\n';
       // }
    }
}
//End of Code Statements
    //BufferFlushed.close();
    return 0;
}

int FindPrimes() {
cout.precision(0);
AllPrimes[0]=2;
double b = 3, s = 2;
int z, c = 1, v = 3000;
        while ( c != v ) {
            z = TestforPrime(b);
            if ( z == 1 ) {
                AllPrimes[c]=b;
                c = c + 1;
                s = s + b;
                if ( c == v ) {
                    cout<<fixed<<" Prime="<<b<<" Count="<<c<<" "<<"Sum="<<s<<"\n";
                    int success = WriteArrayToFile(AllPrimes,3000,"/Users/Yash/Xcode/Projects/Project Euler Programs/Project Euler Problem 60 (Weird Prime Cocatenate Property Problem)/I:O Files/AllPrimes.txt",'n');
                    cout<<"\n Write Success (0=Successful): "<<success<<"\n";
                    if (success == 0) {return 0;}
                    else {return 1;}
                }
                else {

                };
            }
            else {

            };
            b = b + 2;
        }
}

int WriteArrayToFile(int *ArrayPointer,int ArrayLength, string FileName, char Append) {
if (Append == 'y') {
    ofstream OutputFile (FileName, ios::app);
        for ( unsigned long long i1=0 ; i1 < ArrayLength ; i1++) {
            OutputFile<<ArrayPointer[i1]<<"\n";
        }
        OutputFile.close();
        return 0;}
else if (Append == 'n') {
    ofstream OutputFile (FileName);
    for ( unsigned long long i1=0 ; i1 < ArrayLength ; i1++) {
        OutputFile<<ArrayPointer[i1]<<"\n";
        }
        OutputFile.close();
        return 0;}
}

int TestforPrime (double y) {
double x = 2;
while ( x <= y ) {
    if ( (( y / x ) - int( y / x )) == 0 ) {
        if ( y == x ) {
            return 1;
        }
        else {
            return 0;
        }
    }
    x = x + 1;
}
}
4

1 回答 1

0

这个变量:

int Buffer[9000000][5];

占用 45000000 * 4 字节。那是180MB。你不能把它放在堆栈上。使用全局变量或动态分配(或者,更有可能是另一种解决方案 - 我没有查看问题本身,所以不知道您的解决方案是否“正确”)。

于 2013-06-09T08:53:03.300 回答