簡體   English   中英

在我的代碼中出現分段錯誤。

[英]Getting a segmentation fault in my code.

嘗試在Linux上運行程序時,出現分段錯誤。 我是C的新手。在研究過程中,我認為原因可能是循環超出了一個數組中的最后一個索引,並且訪問了它不應該訪問的內存。 有什么提示嗎?


計划概述:接收某種格式的文件,並從該文件中根據選民的三個最喜歡和最不喜歡的地方來確定最佳用餐地點。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int outputToFile (int winner, char **restaurantList, char **voterNameList, int numberOfVoters, int **voterFavorites) {
int index;
FILE *output;

output = fopen ("output_1", "w"); //write mode

fprintf(output, "%s\n", restaurantList[winner]); // write out the winning restuarant
fprintf(output, "Happy:\n");
for (index = 0; index < numberOfVoters ; index++) {              // write out the happy persons
    if (voterFavorites[index][1] == winner || voterFavorites[index][2] == winner || voterFavorites[index][3] == winner) {
        fprintf(output, "%s\n", voterNameList[index]);
    }   
}

fprintf (output, "Sad:\n");
for (index = 0; index < numberOfVoters ; index++) {              // write out the sad persons
    if (voterFavorites[index][4] == winner) {
        fprintf(output, "%s\n", voterNameList[index]);
    }
}

fclose (output);
}

int countScore (int **voterFavorites, int tallyCard[], int numberOfVoters, int numberOfRestaurants) {
int index;

for (index = 0; index < numberOfRestaurants; index++ ) {
    tallyCard[index] = 0;
}

for (index = 0; index < numberOfVoters; index++) {
    if (voterFavorites[index][1] != -999) {
        tallyCard[voterFavorites[index][1]] = tallyCard[voterFavorites[index][1]] + 1;
    } else if (voterFavorites[index][2] != -999) {
        tallyCard[voterFavorites[index][2]] = tallyCard[voterFavorites[index][2]] + 1;
    } else if (voterFavorites[index][3] != -999) {
        tallyCard[voterFavorites[index][3]] = tallyCard[voterFavorites[index][3]] + 1;
    }

    if (voterFavorites[index][4] != -999) {
        tallyCard[voterFavorites[index][4]] = tallyCard[voterFavorites[index][4]] - 1;
    }
}
}

int determineWinner( int tallyCard[], int maxVotes, int **voterFavorites) {
int x, y, max = 0, min = 999, maxIndex, percent; 

for ( x = 0 ; x < 20 ; x++ ) { 
    if (tallyCard[x] > max) {
        maxIndex = x;
        max = tallyCard[x];
    } 
    if (tallyCard[x] < min) {
        if (tallyCard[x] != -999) {
            min = tallyCard[x];
        }
    }
}

percent = max/maxVotes * 100;
if ( percent >= 50) {
    return maxIndex;
} else {
    for(x = 0; x < maxVotes ; x++) {
        for (y = 0; y < 4; y++) {
            if (voterFavorites[x][y] == min) {
                voterFavorites[x][y] == -999;
            }
        } 
    }
    return -444;
}
}

void inputFromFile (void) {
int index, numberOfRestaurants, numberOfVoters, winner;
char *nor, *nov, **restaurantList = malloc(20 * sizeof(char*));;
char **voterNameList = malloc(100 * sizeof(char*));
int **voterFavorites = (int**) calloc(100, sizeof(int*)), tallyCard[20];
int *fav1, *fav2, *fav3, *notFav;
FILE *input;

for ( index = 0; index < 100; index++ ) {
    voterFavorites[index] = (int*) calloc(4, sizeof(int));
    voterNameList[index] = malloc(26 * sizeof(char));
}
for ( index = 0 ; index < 20; index++ ) {
    restaurantList[index] = malloc(51 * sizeof(char));
}

input = fopen ("input_1", "r"); // !!! need to check if this will give an error. Refer to slide set 2-48
if( input == NULL ) { 
    perror("Error while opening the file.\n"); 
    exit(1);
} 

//get names of restaurants
fgets(nor, 51, input);
numberOfRestaurants = atoi(nor); //resolve char * to int
for ( index = 0 ; index < numberOfRestaurants ; index++ ) {
    fscanf(input, "%s", restaurantList[index]);
}

//get info lines and names of voters and initial values
fgets(nov, 101, input);
numberOfVoters = atoi(nov); //resolve char * to int
for ( index = 0 ; index < numberOfVoters ; index++ ) {

    fscanf(input, "%s %i %i %i %i", voterNameList[index], fav1, fav2, fav3, notFav);
    voterFavorites[index][1] = *fav1-1;
    voterFavorites[index][2] = *fav2-1;
    voterFavorites[index][3] = *fav3-1;
    voterFavorites[index][4] = *notFav-1;
}

//count total score for resturants
countScore (voterFavorites, tallyCard, numberOfVoters, numberOfRestaurants);
winner = determineWinner(tallyCard, numberOfVoters, voterFavorites); // !!! probably need to look into these max votes
while (winner < 0) {
    countScore(voterFavorites, tallyCard, numberOfVoters, numberOfRestaurants);
    winner = determineWinner(tallyCard, numberOfVoters, voterFavorites);
}

outputToFile(winner, restaurantList, voterNameList, numberOfVoters, voterFavorites);
}

int main (void) 
{
 inputFromFile();
return 0;   
}

我已經為此工作了一段時間,但我敢肯定這可以清除一點。 對於那個很抱歉!

char *nor, *nov, **restaurantList = malloc(20 * sizeof(char*));;

/* ... */

fgets(nor, 51, input);

nor對象未初始化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM