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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling free on a char * throwing Segmentation fault

Status
Not open for further replies.

dwhalen

Programmer
Feb 22, 2002
105
CA
Hello,

just before my program exits I am trying to free up all malloced memory, however when I try to call free on one of my char * I get a segmentation fault. Here is some of my code.

Here is my structure....it is a linked list:
Code:
//this is a structure for our file information
struct file_info
{
    char *filename;//name of the file
    unsigned long matches;//number of matches for the file
   // struct line_info *
    struct file_info *next_file; //the next file
};

Here is how I add to my structure:
Code:
//this is for the file structure
void add_file(struct word *word_structure, char *line, char *filename, int line_num)
{
 struct file_info *new_file_ptr;
    //allocate our memory
  new_file_ptr = malloc(sizeof(struct file_info));
     if(new_file_ptr == NULL)
     {
         memory_error();
      }

   //printf("Adding file %s\n", filename);
   new_file_ptr->filename = strdup(filename);
   new_file_ptr->filename=filename;//set our file name
   new_file_ptr->matches = 0;//initialize our matches to 0
  //set the next_file to point to the current head
  new_file_ptr->next_file = word_structure->files;
  word_structure->files = new_file_ptr;//now set files to point to our new ptr

}

file_info is part of a another structure called word_structure.


This is where I allocate the memory for my char pointer:
Code:
new_file_ptr->filename = strdup(filename);
So this means that my variable filename has been created from malloc right?

Now, here is how I free everything up:
Code:
void free_files(struct file_info *header)
{
struct file_info *next_file;

    //let's loop through our file list and free up each one
    while(1)
    {

        if(header == NULL)//we have run out of files
            break;

        //set our file list to point to the next file
        next_file = header->next_file;
        printf("Freeing %s\n", header->filename);
        free(header->filename);//error occurs here!!
        free(header);
        header = next_file;
    }

}

Shouldn't calling free on header->filename where filename is a char pointer work?? I was calling malloc myself for the filename pointer but then I saw a book using strdup and thought maybe that was a better way.

Thanks for any help.

Later
 
Here is your problem:
Code:
new_file_ptr->filename = strdup(filename);/*okay*/
new_file_ptr->filename=filename; /*ouch*/

At the end of this little interaction your pointer
is pointing at whatever the filename arg to your
function is pointing to. Obviously in this case
something that can't be freed.
HTH
 
Hey,

Thanks for your reply. I was just going over my code again and noticed the same thing!! Arg, stupid mistake! I meant to comment that line out because that was the old way before I used strdup, but forgot to.

Thanks for your help, this is a great forum.

Later
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top