-3

I'm not quite sure what is wrong with my current program and I have reach a bit of a road block:

(*ptr).Name = (char*)malloc(strlen(record+1));
strcpy((*ptr).Name, record); 
free((*ptr).Name); //problem area

*ptr is a pointer that points to a structure that has various fields. After I copy some data into the Name field I want to free my allocated memory. When I step through my program I get no errors rather just a hanging program that will not continue after I try and free the memory. Any ideas? Thank you.

dreamcrash
  • 40,831
  • 24
  • 74
  • 100
bodotheguy
  • 93
  • 10

1 Answers1

2
(*ptr).Name = (char*)malloc(strlen(record+1)); //This is the problem!
strcpy((*ptr).Name, record); 
free((*ptr).Name); //problem area              //Better practice to use free(ptr->Name

Fix:

ptr->Name = (char*)malloc(strlen(record)+1);   //(record+1) in previouse code was doing 
                                                 //the opposite of what it was intended to do
strcpy(ptr->Name, record); 
free(ptr->Name); 
user253751
  • 50,383
  • 6
  • 45
  • 81
bodotheguy
  • 93
  • 10