-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
  • 47,137
  • 25
  • 94
  • 117
bodotheguy
  • 93
  • 11

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
  • 57,427
  • 7
  • 48
  • 90
bodotheguy
  • 93
  • 11