Why is this free corruption the heap?
Hi there,
Doing something very simple here, not sure why I'm getting heap corruption at run time.
This function was much longer but I stripped it down to figure out what's wrong::
Code:
bool DeleteRecord(void){
StudentRecord** delTemp; // temporary copy of DB used to resize array when adding
delTemp = (StudentRecord**)malloc(g_numRecords * sizeof(StudentRecord*));
for(count=0;count<g_numRecords-1;count++){
delTemp[count]=(StudentRecord *)malloc(sizeof(StudentRecord));
delTemp[count]->firstname=(char *)malloc(sizeof(char));//malloc fname
delTemp[count]->lastname=(char *)malloc(sizeof(char));//malloc lname
}
printf("malloced\n");
delTemp[0]->firstname="........yous";
printf("delTemp[0]->firstname=%s\n", delTemp[0]->firstname);
delTemp[0]->lastname="youtwo";
printf("delTemp[0]->lastnamename=%s\n", delTemp[0]->lastname);
delTemp[0]->id=22;
delTemp[0]->mark=33;
printf("deltemp 0 id, mark: %d, %f\n", delTemp[0]->id,delTemp[0]->mark);
free(delTemp[0]->firstname);
printf("freed fname\n");
free(delTemp[0]->lastname);
printf("freed lname\n");
free(delTemp);
printf("freed deltemp\n");
return true;
}
Code:
struct StudentRecord
{
char* firstname;
char* lastname;
int id;
float mark;
};
I get heap corruption trying to free the memory I just malloced?
Any suggestions?
Thanks!!!