我只有幾天進入C編程。我想重寫一個計算C語言中的素數的小程序,看看它比Java(我來自的編程語言)運行速度快多少。C程序有時會崩潰,有時候不會崩潰
我現在遇到了一個(對我)很奇怪的問題。有時程序工作正常,編譯和終止,但有時它崩潰在「calculateNewPrimes()」函數的某處。 Eclipse不顯示任何錯誤消息,編譯看起來也能正常工作。有沒有人看到這個錯誤,並可以爲我這樣的c-newbie指出來? :)在此網站和其他網頁上的以前的研究沒有帶來任何結果。
全局變量:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
void prepareSession();
void calculateNewPrimes();
void saveResults();
// you can modify the filepath here if you're on a new machine
char filePath[] = "/home/userName/Eclipse/workspace/PrimeNumbers/primes.txt";
// the available system memory or the maxAmount of memory you would like
// to use for the storage of the prime-numbers in BYTES
long long int memorySize = 7000000000LL;
//The file where the prime numbers will be saved
FILE *file;
// We begin with the number 3, so that we only have to check odd numbers.
// 2 is therefore already discovered (amount = 1)
long long int oldAmount = 1;
long long int current = 3;
// a pointer to the array with the already known primes
long long int *knownPrimes;
long long int *newPrimes;
該方案包括3種功能。第一個讀取內存中先前計算的素數,第二個計算新素數,最後一個函數將新計算的素數保存迴文件中。
int main(){
prepareSession();
calculateNewPrimes();
saveResults();
}
函數prepareSession()似乎工作得很好。您可以假定該文件尚不存在,並使用啓動條件。
void prepareSession(){
// try to open the file, if the file doesn't exist it will be created
file = fopen(filePath,"a+");
long long int temp = -1;
fscanf(file,"%lli",&temp);
// if the file isn't empty, read the amount and the highest number in their variables
if(temp != -1){
/*some code */
}
else{
// the file was newly created so the only known prime is 2
printf("File doesn't yet exist. \n");
long long int temp[1] = {2};
knownPrimes = temp;
}
fclose (file);
}
關鍵功能。這裏發生錯誤。
void calculateNewPrimes(){
long long int newAmount;
long long int primesFound = 0;
printf("How many new primes would you like to calculate? \n");
scanf("%lli",&newAmount);
printf("I will calculate %lli new primes. \n",newAmount);
long long int temp[newAmount];
newPrimes = temp;
//prepare measuring
double progress=0.0;
int oldProgress=0;
time_t start = time(0);
long long int currentNumber,oldNumber;
// start the calculations
while(primesFound < newAmount){
oldNumber = 2;
for(long long int i = 1; i < oldAmount + newAmount; i++){
if(i < oldAmount){currentNumber = *(knownPrimes + i);}
else{ currentNumber = *(newPrimes + (i-oldAmount));}
if(current % currentNumber == 0){
break;
}
else if(currentNumber > current/oldNumber || i == oldAmount + primesFound-1){
*(newPrimes + primesFound++) = current;
printf("Found Nr.%lli: %lli \n",primesFound,current);
progress = (primesFound)/(double) newAmount;
while(oldProgress/100.0 < progress){
printf("%d \n",++oldProgress);
}
break;
}
}
current+=2;
}
double timeDifference = difftime(time(0), start);
printf("It took %g to calculate %lli new Primes \n",timeDifference, newAmount);
}
它會崩潰嗎?還是掛起?標題似乎與身體相矛盾。 – Carcigenicate
你做了什麼調試?它在哪裏被卡住**完全** /它崩潰的錯誤是什麼? – Carcigenicate
隨機崩潰通常來自訪問單元化/釋放內存 – Dunno