Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

segmentation fault! (CONT.)

Status
Not open for further replies.

aboujouj83

IS-IT--Management
May 9, 2003
52
US
I am having a "segmentation fault" when running the following program. Please let me know how to correct it. Note that the compilation is error free:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NUM 23
#define STRING_LN 15

/*defining the structures*/
typedef struct{
char first_name[STRING_LN];
char last_name[STRING_LN];
} name;

typedef struct{
name *player_name;
int number;
double salary;
} player;

typedef struct{
name *coach_name;
double salary;
} coach;

typedef struct{
char team_name[STRING_LN];
player team_player[MAX_NUM];
coach *team_coach;
char location[STRING_LN];
} team;

/*******************************************************************
subroutine to create the team
It will invoked directly when the program starts
*******************************************************************/
team* create_team(char *myteam_name, char *mylocation)
{ team *myteam = (team *)malloc(sizeof(team));
int counter; /* used for initializing the array of players */
if (myteam == NULL){
perror(&quot;malloc of myteam&quot;);
exit(-1);
}
/*HERE IS THE PROBLEM- BUT HOW TO CORRECT IT?????*/
strcpy (myteam -> team_name, myteam_name);
strcpy (myteam ->location, mylocation);

/* initialize the array of players */
for (counter = 0; counter < MAX_NUM; counter++){
strcpy(myteam -> team_player[counter] . player_name -> first_name, &quot;&quot;);
strcpy(myteam -> team_player[counter] . player_name -> last_name, &quot;&quot;);
myteam -> team_player[counter] . salary = 0;
}
/* initialize the coach*/
strcpy(myteam -> team_coach -> coach_name -> first_name, &quot;&quot;);
strcpy(myteam -> team_coach -> coach_name -> last_name, &quot;&quot;);
myteam -> team_coach -> salary = 0;
return myteam;
}
.......
The error occurs in the strcpy() method!!!!! when I call it from main():

int main(int argc, char **argv)
{
char answer; /* to read the choice of the user */
team *mteam;
char newtname[STRING_LN]; /* to store newteamname */
char newtlocation[STRING_LN];/* to store newteamlocation */
char pltodelete[STRING_LN]; /* to store temporarily the name
of the player to delete */
if (argc != 3)
Usage (argv[0]);

mteam = (team *)malloc(sizeof(team));
if (mteam == NULL){
perror(&quot;malloc of myteam&quot;);
exit(-1);
}

printf(&quot;\nCreating the team with the arguments specified &quot;
&quot;in the command line\n&quot;);
mteam = create_team(argv[1], argv[2]);
........ THE ERROR OCCURS HERE!!!!!!!! IT SAYS A SEGMENTATION FAULT WHEN TRYING TO CALL STRCPY().IT'S AN ERROR WITH POINTERS AND STRINGS, PLEASE TRY TO FIGURE IT OUT!

GEORGES
 
> mteam = (team *)malloc(sizeof(team));
1. You do this twice - the one in main() is useless, because it gets overwritten by the one in create_team()
2. casting the return result of malloc is not necessary in ANSI-C (it masks a potentially serious problem if you also fail to include stdlib.h)

> if (argc != 3)
> Usage (argv[0]);
Which is good - except that it doesn't actually stop the program from running any further.

My guess is you run the program without arguments, and you get the segfault when you do this
> mteam = create_team(argv[1], argv[2]);
because argv[1] will be NULL and argv[2] will be junk

Or it could be this
Code:
        strcpy(myteam -> team_player[counter] . player_name -> first_name, &quot;&quot;);
        strcpy(myteam -> team_player[counter] . player_name -> last_name, &quot;&quot;);
        myteam -> team_player[counter] . salary = 0;
    }
    /* initialize the coach*/
    strcpy(myteam -> team_coach -> coach_name -> first_name, &quot;&quot;);
    strcpy(myteam -> team_coach -> coach_name -> last_name, &quot;&quot;);
    myteam -> team_coach -> salary = 0;
Each of those second and third level -> operators need to be preceded with an appropriate malloc call.

 
Just two quick comments:

-Salem is correct regarding the usefulness of your malloc() in main(). I would like to point out that it is also a memory leak.

-Code will compile if it is syntactically correct, even if the logic is bad. In other words, the compiler understands the instructions you gave it, but it cannot know if the instructions match your intentions.


Good luck,
Jason
 
Thanks guys for your advice! I have found my mistakes by a careful debugging and following your remarks! My program works perfectly now.
Salem, my code is really long, so I didn't show it all in my previous message. But when I call Usage (argv[0]), the program displays an error message and explains why it fails, then exits.

Georges
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top