繁体   English   中英

为什么我的程序会出现分段错误? 我在堆栈上使用了太多内存吗?

[英]Why does my program reach a Segmentation fault? Am I using to much memory on the stack?

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

#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");
}

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

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

int r = rand() % MAXTEAMS-1; 

r 值可以是-1。 因为 rand()%MAXTEAMS 可能为零,然后它小于 1,你得到一个 -1。

暂无
暂无

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

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