Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Can';在我的斐波那契C+中找不到分段错误的原因+;代码_C++_Segmentation Fault - Fatal编程技术网

C++ Can';在我的斐波那契C+中找不到分段错误的原因+;代码

C++ Can';在我的斐波那契C+中找不到分段错误的原因+;代码,c++,segmentation-fault,C++,Segmentation Fault,我的代码是分段错误。我不明白为什么。这可能与我创建的“fermatPrimeTest()”函数有关 #include <iostream> #include <cmath> #include <cstdlib> #include "fib.h" using namespace std; bool fermatPrimeTest( unsigned long int p ) { if ( p % 2 == 0 ) { // if even retu

我的代码是分段错误。我不明白为什么。这可能与我创建的“fermatPrimeTest()”函数有关

#include <iostream>
#include <cmath>
#include <cstdlib>
#include "fib.h"
using namespace std;

bool fermatPrimeTest( unsigned long int p )
{
  if ( p % 2 == 0 ) { // if even
    return false;
  } 
  else if ( p == 1 || p == 2 || p == 3 ) { // if 1, 2, or 3
    return true;
  }
  unsigned long a = 2 ; // just give it an initial value, make it happy
  for ( int i=0 ; i < 3 ; i++) {
    if (GCD(a, p-1) != 1) {
      a = rand() % (p-1) + 1;
    }
    // cout << "a=" << a << " ,p=" << p << GCD(a, p-1) << endl;
  }
  return true;
}

int GCD(unsigned long int a, unsigned long int b) {
  while( 1 ) {
    a = a % b;
    if( a == 0 )
      return b;
      // cout << "GCD-b=" << b << ", GCD-a=" << a << endl;

    b = b % a;

    if( b == 0 )
      return a;
      // cout << "GCD-a=" << a << ", GCD-b=" << b << endl;
  }
}

// fills array with Fibonacci primes
// returns number of primes
unsigned long findFibonacciPrimes (unsigned long lowerBound,
                                   unsigned long upperBound,
                                   unsigned long result[])
{  
  int i = 0;  //counter
  unsigned long maxElements = upperBound - lowerBound + 1;  // there can be no more array elements than this

  /*
  The array result is guaranteed to have enough space to store all the numbers found. 
  Your program will crash if it attempts to write past the end of the array, and no 
  marks will be awarded for any tests in which this occurs. You are also guaranteed that 
  1 < lowerBound < upperBound < 3,000,000,000.
  Every F_n that is prime has a prime  index n, with the exception of F_4=3. 
  However, the converse is not true (i.e., not every prime index p gives a prime F_p). 
  */

  unsigned long int one = 0, two = 1, three = one + two;   // element x-2, x-1, and x for Fib sequence
  result[i++] = 0;
  result[i++] = 1;

 while ( lowerBound < three < upperBound ) {
    if ( fermatPrimeTest(three) ) {
      result[i++] = three;
    }
    one = two;
    two = three;
    three = one + two;
  }
  return sizeof result / sizeof result[0];   // return size of array TODO!
}
int main() {
  unsigned long result[100];
  findFibonacciPrimes(1,100,result);
}
#包括
#包括
#包括
#包括“fib.h”
使用名称空间std;
布尔费马检验(无符号长整数p)
{
如果(p%2==0){//if偶数
返回false;
} 
else如果(p==1 | | p==2 | | p==3){//如果1、2或3
返回true;
}
unsigned long a=2;//只要给它一个初始值,就可以了
对于(int i=0;i<3;i++){
如果(GCD(a,p-1)!=1){
a=rand()%(p-1)+1;
}
//cout第67行:

 while ( lowerBound < three < upperBound ) {

我看到了很多潜在的bug

以下是导致崩溃的错误:

考虑到您的主循环:

 while ( lowerBound < three < upperBound ) {
    if ( fermatPrimeTest(three) ) {
      result[i++] = three;
    }
    one = two;
    two = three;
    three = one + two;
  }
sizeof(result)将始终返回4(或64位编译时返回8),因为您计算的是指针的大小,而不是静态数组的sizeof。编译器不知道此指针实际上是静态大小的数组

你可能想说:

return i;

不要让人们去另一个网站。你为什么不使用向量,看起来更安全。我认为素数的定义是一个除1和它本身外,不可被任何其他数字整除的数字。在这种情况下,1不能被任何数字整除,除了它本身和1。素数是一个正整数,只有一个正除数,而不是1()@Felipe:“素数”只是一个词,在数学中它的意思是某种东西。事实上,它曾经是1被认为是素数,但现在不是了。2是最小的素数。@NullUser在删除的答案中,它是编译的。它与
(lowerBound
。我们从
lowerBound
得到一个布尔值
b
,给出
b
b
将通过
b?1:0
提升为整数,给出
1
0
。哈哈,这一个搞笑的错误!这一个将坚持数组。我们有我还没有覆盖向量。
 while ( lowerBound < three < upperBound ) {
    if ( fermatPrimeTest(three) ) {
      result[i++] = three;
    }
    one = two;
    two = three;
    three = one + two;
  }
 while ((i < maxElements) && (lowerBound < three) && (three < upperBound)) {
    if ( fermatPrimeTest(three) ) {
      result[i++] = three;
    }
    one = two;
    two = three;
    three = one + two;
  }
return sizeof result / sizeof result[0];   // return size of array TODO!
return i;