2015-03-30 130 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

相關問題