Reputation: 91
I have a structure to store information of employees
struct employeeData
{
char name[50];
int age;
char rank[50];
char level[50];
char memo[100];
};
I save them into employeeDataArr[size]
using malloc in my enterNew function.
Now, I am trying to remove certain employees from the employeeDataArr[size]
using their name.
So, I used nameIndex function to see if the inputted named employee exists in the data base. Once the person exist, I am trying to use
free(employeeDataArr[size]);
to remove the information of a certain employee.
I expected to remove their name, age, rank, level and memo for that certain employees. However, It seems like it only removes the name of the employees.
void removeRecord (struct employeeData * dataBase[])
{
printf("enter employee name");
int nameIndex
char employeeName[50];
scanf(%s, employeeName)
if(check if the name exists in the data base)
{
printf("Error, no such person exists");
}
else
{
nameIndex = nameIndex(employeeName,dataBase);// search the index of inputted person's data in the dataBase array
free(dataBase[nameIndex]);//free the memory of inputted person's data from the data base(this only removes the name of the person's data)
}
When I do this, it gives me like this.
before removal
Name: Test 1
Age: 43
Rank: supervisor
level: 2I
memo: testMemo
After removal
Name:
Age: 43
Rank: supervisor
level: 2I
memo: testMemo
As you can see, only the name data from the actual structure. I thought free would free the memory that i allocated previously which will remove all data of the structure.
It seems like I am not using the free in the way it should. What would be the issue here?
Thank you for your time
Upvotes: 0
Views: 537
Reputation: 59
Yes. You can set after free() the pointer to NULL. Than it is a little bit clearer. You can also check the allocated memory bevor and after free() .
Upvotes: 1
Reputation: 71
free method in C just tells the operating system that it can now use this piece of memory, this does not mean that the data will actually be deleted.
Upvotes: 1