分段错误C ++(数组太大了吗?) [英] Segmentation Fault C++ (array too large?)

查看:130
本文介绍了分段错误C ++(数组太大了吗?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的项目欧拉问题14 ,在那里我需要找到时间最长的在Collat​​z序列根据1,000,000。我想出了对于人数较少的工作(比如,100),从各1个在Collat​​z数存储算法 - 100到一个数组中,并使用该数组作为参考,以加快数字较高的计算。
我的code是如下:

I'm working on the Project Euler Problem 14, where I need to find the longest collatz sequence under 1,000,000. I've come up with an algorithm that works for smaller numbers (say, 100) that stores each collatz number from 1 - 100 into an array and uses that array as a reference to speed up the computations for higher numbers. My code is as follows:

#include <iostream>
using namespace std;

long even(long n){ //the even-collatz function
    n=n/2;
    return n;
}

long odd(long n){ //the odd collatz function
    n=3*n+1;
    return n;
}

int main(){
    long  x, c=0, y[1000000]; // x= the number we are finding the collatz number of, c a counter that keeps track of how many steps we've taken in the sequence, y is an array to store the collatz numbers.

    for (x=1; x<1000000; x++){ //iterates from x=1 to 1 million
            long a = x;     //sets a number a equal to the number we are currently trying to find the collatz number of
            long b = a;     
            c=0;                    //intializes counter at 0 
            while (a!=0){           //loops infinitely; the only way to exit is through a break.
                    if (a%2==0){    // detects if the number is even
                            a=even(a);      //applies the even-collatz function if so; sets x=x/2
                            c=c+1;
                            if (y[a]!=0){   // checks if the collatz number of x is already discovered
                                    y[b]=c+y[a]; //adds the current number of steps to the collatz number of x and 
                                    break;  //exits the while loop
                            }

                    }
                    else if (a==1){         //checks if the new x is equal to one and
                            y[b]=c;         //if it is, it writes the current value of c to y[b] and
                            break;          // exits the loop
                    }
                    else if (a%2==1){       //same as the "even" block, except for odd numbers 

                            a=odd(a);
                            c=c+1;
                            if( y[a]!=0){
                                    y[b]=c+y[a];
                                    break;
                            }

                    }
            //this is the end of the while loop; we've applied the collatz function as many times as we've needed to to x, and incremented the counter each time
            }
  }

    long z;
    for (int n=0;n!=100;n++){
            if (y[n+1]>y[n]){
                    z=y[n+1];
            }
    }
    cout << z << "\n";


}

我遇到的问题是,我得到在x = 1818段错误的for循环。经过调试,我发现,如何迅速发生段错误取决于阵列y的大小,所以我假设数组只是太大。从段错误的我(基本)的了解,我想我只是访问了我不得入内的记忆。有没有什么办法,我绕过这个,或者我应该刚开始走向另一个解决这个问题的工作吗?我使用Ubuntu的工作室的G ++编译。

The issue I'm having is that I get a segfault after x=1818 in the for loop. Through debugging, I've found that how quickly the segfault occurs depends on the size of array y, so I'm assuming that the array is just too big. From my (basic) understanding of segfaults, I think I'm just accessing memory that I'm "not allowed". Is there any way for me to circumvent this, or should I just start working towards another solution to this problem? I'm compiling using g++ on Ubuntu studio.

推荐答案

这阵可能是太大了系统的缺省堆栈大小;最简单的解决方法是它的定义更改为:

This array is probably too big for your system's default stack size; the simplest fix is to change its definition to:

std::vector<long> y(1000000);

和其他一切可以保持不变。你可以使用 y.size()而不是幻数百万后来在你的循环。

and everything else can stay the same. You could use y.size() instead of the magic number 1000000 later in your loop.

这篇关于分段错误C ++(数组太大了吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆