user7847469
user7847469

Reputation:

Freeing array of structures

I'm trying to free an array of structures, which inside holds an array of strings, which they are all allocated with malloc and the program works until I try to free it with the function I made , and I get core dumped.

this is my structures: name-allocated array ,courses -array of strings , and teach is an allocated array of structures which I'm trying to free one by one.

 struct date

{
    int day,month,year;
};
struct lecturer
{
char * name;
struct date birthdate;
int num_of_courses;
char ** courses;
};

this is my function:

void freeAndExit(struct lecturer* teach,int num)
{
  int i,j;
  for(i=ZERO;i<num;i++)
  {
    free(teach[i]->name);
    for(j=ZERO;j<teach[i]->num_of_courses;j++)
        free(teach[i]->courses[j]);
  }

free (teach[i]);
}

this is how I call my function from my main()

freeAndExit(&teach,numTeach);

any ideas? Edit: this is How I inputed my structs

void InputLecturer(struct lecturer** teach,int num)
{
    int i;
    char temp[SIZE];
    getchar();
  teach=(struct lecturer*)malloc(sizeof(struct lecturer)*num);
   for(i=ZERO;i<num;i++)
   {
     printf("Please enter the lecturers name:\n");
     InputStr(temp);
     (*teach)[i].name=(char*)malloc(sizeof(char)*strlen(temp));
     strcpy((*teach)[i].name,temp);
     printf("Please enter the birtday date:|Day|Month|Year|\n");
     scanf(" %d %d %d",&(*teach)[i].birthdate.day,&(*teach)[i].birthdate.month,&(*teach)[i].birthdate.year);
     printf("Enter the number of courses\n");
     scanf("%d",&(*teach)[i].num_of_courses);
     getchar();
     InputCourses(&(*teach)[i],(*teach)[i].num_of_courses);

   }


}
void InputCourses(struct lecturer* teach,int num)
{
    int i;
    char temp[SIZE];
   teach->courses=(char**)malloc(sizeof(char)*num);
   for(i=ZERO;i<num;i++)
   {
      printf("Please enter course name number %d\n",i+1);
      InputStr(temp);
      teach->courses[i]=(char*)malloc(sizeof(char)*strlen(temp));
      strcpy(teach->courses[i],temp);
   }

}

Upvotes: 0

Views: 82

Answers (1)

Chris Turner
Chris Turner

Reputation: 8142

First off, this line is outside of the loop so the value of i would be outside the bounds allocated for teach. Secondly, teach[i] is a struct lecturer - so it's not allocated memory anyway, so nothing to free.

free (teach[i]);

What is needing to be freed is teach itself so replace the above line with this one.

free (teach);

You should also make sure you allocate enough memory for any strings - they always need one more than you think you need to store the terminating NUL character. So, for example, this line

teach->courses[i]=(char*)malloc(sizeof(char)*strlen(temp));

should be

teach->courses[i]=malloc(sizeof(char)*(strlen(temp)+1));

(note: you shouldn't need to cast the return value of malloc in C)

Not allocating enough space for the string invokes undefined behaviour as when you strcpy the string into the newly allocated memory, the NUL terminating character is going to be written beyond the bounds of memory you've been allocated and which could be later overwritten by some legitimate owner.

Alternatively you could combine the malloc and strcpy calls into one call to strdup which allocates the right amount of memory and copies the existing string for you...

teach->courses[i]=strdup(temp);

Upvotes: 1

Related Questions