为什么我的程序会出现Segmentation故障?我是否在堆栈上使用了很多内存?

时间:2015-11-15 12:35:58

标签: c memory-management simulation

我似乎无法进一步调试我的程序。有时它完成没有任何问题,有时我会在一段时间后(在随机点)得到分段错误。我猜我正在使用我的char* nbateams[]数组在堆栈上使用大量内存。我可能应该在堆上malloc一些内存来替代它。或者您怎么看?如果是这种情况,我需要分配多少内存?

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

// constants for teams in the leauge, max points and minimum points.
#define MAXTEAMS 30
#define MAX_P 110
#define MIN_P 70

//structure for the "gameday"
struct gameday
{
    char* team;
    int points;
};

// prototypes
int scoreGenerator();
int teamGenerator();
void anouncement(int n);

int main(void)
{
    // all the teams. +1 to reserve space for null character
    char* nbateams[MAXTEAMS+1] = {"Brooklyn Nets", "New York Knicks", "Philadelphia 76ers", "Toronto Raptors", "Boston Celtics",
                                  "Chicago Bulls", "Cleveland Cavaliers", "Detroit Pistons", "Indiana Pacers", "Milwaukee Bucks",
                                  "Atlanta Hawks", "Charlotte Hornets", "Miami Heat", "Orlando Magic", "Washington Wizards",
                                  "Denver Nuggets", "Minnesota Timberwolves", "Oklahoma City Thunder", "Portland Trail Blazers", "Utah Jazz",
                                  "Golden State Warriors, Los Angeles Clippers, Los Angeles Lakers, Phoenix Suns, Sacramento Kings",
                                  "Dallas Mavericks", "Houston Rockets", "Memphis Grizzlies", "New Orleans Pelicans", "San Antonio Spurs"};

    // how many games do you want to be played? 
    int nbagames = 0;

    while(nbagames < 10)
    {
        // gets the first team and generates a score for it
        struct gameday team1;
        team1.team = nbateams[TeamGenerator()];
        team1.points = ScoreGenerator(team1.team);

        // gets a second team and generates score for it. Keep generating untill the teams are not the same
        struct gameday team2;
        do
        { 
            team2.team = nbateams[TeamGenerator()];

        } while(strcmp(team1.team, team2.team) == 0);

        // gets points for team2 untill they are not equal to team1s points.
        do
        { 
            team2.points = ScoreGenerator(team2.team);

        } while(team1.points == team2.points);

        // prints out the diffrent teams and their points
        printf("\n");
        printf("%s(%d) - %s(%d)\n", team1.team, team1.points, team2.team, team2.points);


        // get data for anouncement, so we can print out the winner and such.
        int n = 0;
        if (team1.points > team2.points)
        {
            n = team1.points - team2.points;
            printf("The %s ", team1.team);
            anouncement(n);
        }
        else
        {
            n = team2.points - team1.points;
            printf("The %s ", team2.team);
            anouncement(n);
        }
        printf("\n");
        // increment the nba games played.
        nbagames++;
    }
}
// get psudorandom score based on the time and the leanght of the team name.
int scoreGenerator(char* team)
{
    srand(time(NULL) + strlen(team));

    int score = rand() % (MAX_P  - MIN_P + 1) + MIN_P;

    return score;
}
// get psudorandom team.
int teamGenerator(void)
{
    srand(time(NULL));

    int r = rand() % MAXTEAMS-1;

    return r;
}
// print out an extra message dependent on how much the team won the game with.
void anouncement(int n)
{
    if(n <= 5)
        printf("win the close game");
    else if(n <= 15)
        printf("win the game");
    else
        printf("win the blowout game");
}

编辑:我想我发现了我的愚蠢错误......在我的数组中,我有一些缺少的引号。

1 个答案:

答案 0 :(得分:0)

BLUEPIXY和mch说是对的。还有另一个错误。

int r = rand() % MAXTEAMS-1; 

r值可以是-1。因为rand()%MAXTEAMS可能为零,然后是sub 1,你得到-1。