繁体   English   中英

CS50 - PSET3 复数 - 分段错误

[英]CS50 - PSET3 Plurality - Segmentation fault

#include <stdio.h>
#include <string.h>
// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct
{
    string name;
    int votes;
}
candidate;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;

// Function prototypes
bool vote(string name);
void print_winner(void);

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: plurality [candidate ...]\n");
        return 1;
    }

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX)
    {
        printf("Maximum number of candidates is %i\n", MAX);
        return 2;
    }
    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;
    }

    int voter_count = get_int("Number of voters: ");

    // Loop over all voters
    for (int i = 0; i < voter_count; i++)
    {
        string name = get_string("Vote: ");
        // Check for invalid vote
        if (!vote(name))
        {
            printf("Invalid vote.\n");
        }
    }

    // Display winner of election
    print_winner();
}

// Update vote totals given a new vote
bool vote(string name)
{
    // TODO
    for (int i = 0; i <= candidate_count -1 ; i++)
    {
        if (strcmp(name, candidates[i].name) == 0)
        {
            candidates[i].votes = candidates[i].votes + 1;
            return true;
        }
    }
    return false;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    // TODO
    candidate temp[0];
    for (int i = 0; i < candidate_count; i++)
    {
        if((candidates[i].votes > candidates[i + 1].votes) && (i + 1 < candidate_count))
        {
            temp[0] = candidates[i];
            candidates[i] = candidates[i+1];
            candidates[i+1] = temp[0];
            i = -1;
        }

    }

for (int i = 0; i <= candidate_count; i++)
    {
        if (candidates[candidate_count - 1].votes == candidates[candidate_count - i].votes)
        {
            printf("%s\n",candidates[candidate_count - i].name);
        }
    }

}


您好,感谢您抽出宝贵的时间阅读我的问题:)

我刚刚进入 CS50,非常感谢 David Malan 的教学方式。 我之前没有真正的编程经验,尽管我在学校期间不得不编程一点。

我在其他线程中寻找这个答案,但没有找到相应的答案。 如果我监督了可能的答案,请随时纠正我。

现在的问题如下:当我运行这段代码时,它工作正常。 它按应有的方式打印所有内容。 除了在大多数情况下,在一切正常之后,它在给出正确的 output 后返回分段错误(核心转储)。

我假设我引用了一个不存在但无法发现的数组索引。 调试也无济于事,因为它在 main 的最后一个大括号中显示了错误。

感谢您的时间:)

您的可疑代码行可能是:

    if((candidates[i].votes > candidates[i + 1].votes) && (i + 1 < candidate_count))

因为这超出了候选人数组的末尾。 只是为了检查一下,我添加了一个快速测试来查看该区域并打印它。

if (i == candidate_count -1)
    printf("Suspect code ahead: name-> %s, votes: %d\n", candidates[i + 1].votes);
        if((candidates[i].votes > candidates[i + 1].votes) && (i + 1 < candidate_count))

当我在我的机器上运行三个候选人的程序时,这是终端 output。

@Una:~/C_Programs/Console/Candidate/bin/Release$ ./Candidate A B C
Number of voters: 5
Vote: A
Vote: B
Vote: B
Vote: B
Vote: C
Suspect code ahead: name-> (null), votes: -1921036152
B

在我的例子中,程序没有遇到段错误,但它也可以读取越界 memory。 您可能需要做的是一些边界检查,这样您就不会尝试读取您的数组。

检查出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM