I'm just starting to program in C, and my program is being difficult. I am working on a text parser to make it easier to do some sabremetrics on the teams in my fantasy baseball league (woo!). Generally, I am parsing specific pieces of a text file into an array of structs which store a string. The structs are defined as:
and initialized as:
The team names are then set in a while loop by:
#define NUMTEAMS 12
struct team_roster_t* teams = malloc(NUMTEAMS*sizeof(struct team_roster_t));
I'm running into the peculiar problem that when my program executes the malloc for the fourth team name (Quaker Goats), it additionally appends an exclamation point to the third team name (Biddlestar Galactica --> Biddlestar Galactica!). I know via gdb that the team_name field for the Biddlestar is set correctly when it is initialized, without an exclamation point. I also know that the ! appears as soon as malloc() is called a second time, before the fourth team name ("Quaker Goats") is copied (via strcpy) to the newly malloc()ed address.
/*char* team is set with some file and string functions that I know correctly retrieve the team name.*/
int namesize = strlen(team);
teams[i].team_name = malloc(namesize);
There are no exclamation points anywhere in the input .txt file, nor are there any strings with an "!" in my code. Since the '!' appears following an unrelated malloc, I'm assuming it's some sort of memory leak, but I can't seem to find any indication that an ! would result from this sort of error.
I'm happy to post more code or answer anything I wasn't clear about. Hope someone can help!