Are there any memory leaks here??
Code:
bool AddRecord(StudentRecord**& Records, int& numRecords){
/*Please tell me if I have memory leaks.*/
/*
struct StudentRecord
{
char* firstName; // student key
char* lastName; // student name
int id; // student id
float mark; // course mark
};*/
char FirstName[MaxRecord];char LastName[MaxRecord];int id; float mark;
StudentRecord**Temp; /*Declare array of pointers to a struct*/
Temp=new StudentRecord*[numRecords];
cout<<"Please enter First Name:\n";
cin>>FirstName;
cout<<"Please enter Last Name:\n";\
cin>>LastName;
cout<<"Please enter ID:\n";
cin>>id;
cout<< "Please enter mark\n";
cin>>mark;
numRecords++;
for (int i=0; i<numRecords;i++)
{
/*dynamically allocate memory*/
Temp[i] = new StudentRecord; // allocate space for student record
Temp[i]->firstName=new char[strlen(FirstName)+1];
Temp[i]->lastName=new char[strlen(LastName)+1];
strcpy(Temp[i]->firstName,FirstName);
strcpy(Temp[i]->lastName,LastName);
Temp[i]->id=id;
Temp[i]->mark=mark;
}
if (numRecords==1)
{
Records=new StudentRecord*[numRecords];
for (int j=0;j<numRecords;j++)
{
Records[j] = new StudentRecord;
Records[j]->firstName=new char[strlen(Temp[j]->firstName)+1];
Records[j]->lastName=new char[strlen(Temp[j]->lastName)+1];
strcpy(Records[j]->firstName,Temp[j]->firstName);
strcpy(Records[j]->lastName,Temp[j]->lastName);
Records[j]->id=Temp[j]->id;
Records[j]->mark=Temp[j]->mark;
}
}
else if (numRecords>1)
{
for (int k=numRecords-1, l=0; l<numRecords, k<numRecords;k++,l++)
/*get ready to write from last records in studentrecord** Records by copying
studentrecord **temp starting at 0*/
{
Records[k] = new StudentRecord;// allocate space for student record
Records[k]->firstName=new char[strlen(Temp[l]->firstName)+1];
Records[k]->lastName=new char[strlen(Temp[l]->lastName)+1];
strcpy(Records[k]->firstName,Temp[l]->firstName);
strcpy(Records[k]->lastName,Temp[l]->lastName);
Records[k]->id=Temp[l]->id;
Records[k]->mark=Temp[l]->mark;
}
}
//delete []Temp; /*crashes when I use this*/
return true;
}