2015-03-30 90 views
-2

如果我在dicksons方法函数中注释掉printf行,则会出现分段错误。如果我将它放在代码运行中并产生正确的答案。为什么? 我已经包括了所有的代码,因为只有一部分可能会当我注释掉printf语句时,为什么会出现分段错误

#include<stdio.h> 
#include <time.h> 
#include <math.h> 

int bruteforce(int max){ 
} 
int bruteforcerefined(int max){ 
} 

int dicksonsmethod(int max){ 
printf("The Dickson method\n");// If I comment out this line I get a Segmentation fault. 
int r,dm,fp; 
int a=1,b=2,c=3; 
int perimeter,multiplier; 
int paircount=0; 
int factorpairs[2][20]; 

for (r=2;;r+=2){ // infinite loop 
    dm=r*r/2; 

    paircount=0;// Get the count of factor pairs, and the pairs of factors 
    for (fp=1;fp<(dm/2)+1;fp++){ 
     if (dm%fp==0){ 
      if(fp >=dm/fp){ // fp >=dm/fp to avoid duplicating pairs in reverse 
       break; // do not want to get reverse facor pairs 
      } 
      paircount+=1; 
      factorpairs[paircount][0]=fp; 
      factorpairs[paircount][1]=dm/fp; 
     } 
    } 
    for (fp=1;fp<=paircount;fp++){ // for each Dickenson pair 
     a=r+factorpairs[fp][0]; // get the pythagorean triplet 
     b=r+factorpairs[fp][1]; 
     c=r+factorpairs[fp][0]+factorpairs[fp][1]; 
     perimeter=a+b+c; 
     if(max%perimeter==0){ // and see if it will scale to required size 
      multiplier=(int)max/perimeter; 
      //   printf("Sides %d %d %d as a triplet, the sum being %d, from factor pairs %d and %d \n",a,b,c,perimeter=a+b+c,factorpairs[fp][0],factorpairs[fp][1]); 
      return a*b*c*multiplier*multiplier*multiplier; 
     } 
    } 
} 
} 

int main() 
{ 
int max=1000; 
char str; 
while (1){ 
printf("Compare various methods of finding Pythagorean Triples to fullfil the specified conditions\n"); 
printf("1 - The Brute Force method.\n"); 
printf("2 - The Brute Force method, refined.\n"); 
printf("3 - The Dickson\'s method.\n"); 
printf("The Fibbonacci method, does not product usable triplets in a timely manner.\n"); 
printf("The Squared Difference, special method, does not product usable triplets in a timely manner.\n"); 
printf("The Squared Difference, general method, does not product usable triplets in a timely manner.\n"); 
printf("Enter a number 1 - 3 to execute or E to exit:- "); 
scanf(" %c",&str); 
printf("\n\n\n"); 
clock_t begin, end; 
double time_spent; 
begin = clock(); 
/* here, do your time-consuming job */ 
if (str == '1') { 
    printf("Brute force produces %d\n",bruteforce(max)); 
} 
if (str == '2') { 
    printf("Checking multiples of triplets produces %d\n",bruteforcerefined(max)); 
} 
if (str == '3') { 
    printf("The Dickson\'s method produces %d\n",dicksonsmethod(max)); 
} 
end = clock(); 
time_spent = (double)(end - begin)/CLOCKS_PER_SEC; 
printf(" and took %f seconds",time_spent); 
if(str == 'E' || str == 'e'){ 
    return 1; 
}printf("\n\n\n"); 
} 
} 
+0

您可能正在访问不属于您的内存。 – immibis 2015-03-30 23:08:55

+0

您是否尝试过在调试器中运行它以查找崩溃的行? – teppic 2015-03-30 23:44:30

回答

0

你破坏堆栈,它导致崩溃。我没有通过你的算法,但显而易见的事情是你的factorpairs阵列被写出界限。快速调试看paircount产生对我来说:

Old value = 1 
New value = 2 
dicksonsmethod (max=1000) at a.c:32 
32   factorpairs[paircount][0] = fp; 

即你在某一时刻函数试图写出界,因为paircount已成为2.

它与printf运行的原因是因为你'在未定义的行为领域。

+0

谢谢。通过改变int因子对[2] [20];整数因子对[5] [20];问题就消失了。 – 2015-03-31 09:48:15

相关问题