c - 运行测试时在我的 C 程序中出现分段错误
问题描述
/* Created Library File */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include "tournament.h"
Team * initializeTeam(char * name, Player * players){
Team *team = malloc(sizeof(Team));
(*team).name = name;
(*team).players = players;
return team;
}
Team * game(Team * team1, Team * team2){
int turns = 10;
int team_A_total = 0;
int team_B_total = 0;
int team_A_shooting = 0;
int team_B_shooting = 0;
int team_A_blocking = 0;
int team_B_blocking = 0;
int team_A_def = 0;
int team_B_def = 0;
int team_B_off = 0;
int team_A_off = 0;
for(int i=0;i<turns;i++){
team_A_off = team_A_off + (*team1).players[i].offensive;
team_A_def = team_A_def + (*team1).players[i].defensive;
team_B_off = team_B_off + (*team2).players[i].offensive;
team_B_def = team_B_def + (*team2).players[i].defensive;
};
for(int j=0;j<turns;j++){
team_A_shooting = rand() % team_A_off;
team_A_blocking = rand() % team_A_def;
team_B_shooting = rand() % team_B_off;
team_B_blocking = rand() % team_B_def;
if(team_A_shooting>team_B_blocking){
team_A_total = (team_A_total + 1);
};
if(team_B_shooting>team_A_blocking){
team_B_total = (team_B_total + 1);
};
};
if(team_A_total > team_B_total){
return team1;
}
else
if(team_B_total > team_A_total){
return team2;
}
else
if(team_A_total == team_B_total){
int number = rand();
if(number % 2 == 0){
return team1;}
else{
return team2;}
}
return 0;
}
/*header file*/
#ifndef JG_TOURNEY_H
#define JG_TOURNEY_H
typedef struct Player{
int offensive;
int defensive;
int number;
int team;
char first[255];
char last[255];
} Player;
typedef struct Team{
char * name;
Player * players;
} Team;
Team * initializeTeam(char * name,Player * players);
Team * game(Team * team1, Team * team2);
Team * tournament(Team **, int);
void deleteTeam(Team *);
#endif
/*Driver code*/
#include"tournament.h"
#include"tournament.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#define DEBUG 0
#define NUM_TEAMS 32
#define NUM_PLAYERS 10
Player * draftPlayers(char * filename, int team, int num_players);
int inLeague(Team *, Team **);
int main(){
/***** Change the team names to names of your choosing ******/
char * team_names[] = {
"team1",
"team2",
"team3",
"team4",
"team5",
"team6",
"team7",
"team8",
"team9",
"team10",
"team11",
"team12",
"team13",
"team14",
"team15",
"team16",
"team17",
"team18",
"team19",
"team20",
"team21",
"team22",
"team23",
"team24",
"team25",
"team25",
"team27",
"team28",
"team29",
"team30",
"team31",
"team32",
};
/********************************************************************/
/* DO NOT ALTER ANY OF THE THE DRIVER CODE BELOW */
/********************************************************************/
#if !DEBUG
fclose(stderr);
#endif
printf("\t-----------------------------------\n");
printf("\t- Creating Your League -\n");
printf("\t-----------------------------------\n");
srand(time(NULL));
printf("\n\t=========Test #1: Creating your Teams and Players===========\n\n");
Team ** league = malloc(sizeof(Team*) * NUM_TEAMS);
//create teams i.e. league of N teams.
for(int counter = 0; counter < NUM_TEAMS; counter++){
Player * draft = draftPlayers("players.dat", counter, NUM_PLAYERS);
assert(draft[0].team == counter%NUM_TEAMS);
league[counter] = initializeTeam(team_names[counter], draft);
assert(sizeof((league[counter]->players[rand() % NUM_PLAYERS])) == sizeof(Player));
assert((league[counter]->players[rand() % NUM_PLAYERS]).offensive > 0);
assert((league[counter]->players[rand() % NUM_PLAYERS]).defensive > 0);
assert(sizeof(*(league[counter])) == sizeof(Team));
}
printf("\n\t\t....League Initialized\n");
printf("\t-----------------------------------\n");
printf("\t- Part 1: Exhibition Games -\n");
printf("\t-----------------------------------\n");
printf("\n\t=========Test #1: Sanity check for the game===========\n\n");
Team * winner = game(NULL, NULL);
assert(winner == NULL);
printf("\n\t\t....Test Passed\n");
printf("\n\t=========Test #2: Playing a Game between two random teams===========\n\n");
Team * team1 = league[rand() % NUM_TEAMS];
Team * team2 = league[rand() % NUM_TEAMS];
printf("Up next, an exhibition game between %s and %s\n", team1->name, team2->name);
winner = game(team1, team2);
assert(winner == team1 || winner == team2);
printf("The winning team is %s\n\n", winner->name);
printf("\n\t\t....Test Passed\n");
printf("\n\t=========Test #3: Playing a Game between the same team===========\n\n");
int team_num = rand() % NUM_TEAMS;
printf("Up next, a a scrimmage for %s\n", league[team_num]->name);
winner = game(league[team_num], league[team_num]);
assert(winner == league[team_num]);
printf("The winning team is %s\n\n", winner->name);
printf("\n\t\t....Test Passed\n");
当运行创建的库文件并使用驱动程序代码运行它时,由于分段错误,我第一次测试失败。我知道这与我的记忆有关,但是我无法弄清楚我的代码在哪里导致了这种情况。我知道要处理的代码很多,但是我只包含了运行此测试所需的代码部分。非常感谢任何帮助。
解决方案
(*team).name = name;
(*team).players = players;
看起来您正在尝试使用 取消引用team
,*
然后使用 再次取消引用.name
,因此请尝试仅使用or删除*
和取消引用。.name
->name
推荐阅读
- kubernetes - Traefik 2 不匹配 2 个入口点
- jupyter-notebook - 错误:“numpy.ndarray”对象没有属性“iloc”
- python - 不能将按钮放在另一个框架内的框架内
- javascript - Chart.js angular8 Y 轴刻度
- java - java变量范围如何在这里工作?
- excel - 用户输入登录然后继续通过宏指令可能吗?
- ios - 如何从objective-c中的类名中获取静态/可重定位的框架名称
- c# - .Net C# 依赖注入解析 ICommands 相同类型
- java - MongoDb Java 驱动 toJson() 和 $oid
- node.js - 尝试使用 npx create-react-app 创建反应应用程序时出错