Thread: malloc appends an exclamation point?

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    malloc appends an exclamation point?

    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:

    Code:
    struct team_roster_t{
       char* team_name;
    }team_roster_t;
    and initialized as:

    Code:
    #define NUMTEAMS 12
    ...
    struct team_roster_t* teams = malloc(NUMTEAMS*sizeof(struct team_roster_t));
    The team names are then set in a while loop by:

    Code:
    /*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);
    strcpy(teams[i].team_name, team);
    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.

    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!

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    int namesize = strlen(team);
    teams[i].team_name = malloc(namesize);
    strlen() does not include terminating null character.
    And strlen() returns size_t.
    Code:
      size_t namesize = strlen(team) + 1 ; // +1 for '\0'
      team[i].team_name = malloc(namesize);
      if( team[i].name_name == NULL )

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    2
    Of course! Still not sure how an exclamation point got there--I'm sure it's some artifact of the ascii code and the header for the next string. Won't worry too much about it though. Thanks!

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    This time it may be exclamation point. next time it may be '?'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 04:55 PM
  2. Singly linked lists tutorial, problem with malloc
    By Protolocke in forum C Programming
    Replies: 13
    Last Post: 04-23-2010, 11:14 PM
  3. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 01:06 PM
  4. const Point& and const Point are same
    By noobcpp in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2007, 01:39 AM
  5. more with my 1st structure program
    By sballew in forum C Programming
    Replies: 42
    Last Post: 10-22-2001, 08:03 PM

Tags for this Thread