gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:41:17 24 4
gpt4 key购买 nike

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

#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。

关于c - 为什么我的程序会出现段错误?我在堆栈上使用了太多内存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33719723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com