0

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

3
  • You can't really delete an element from an array in C. You should shift the array values so that it seems like it was deleted. This can help. Commented Apr 4, 2021 at 14:23
  • If your array is created with malloc, you could move objects down one index then realloc with new size. Commented Apr 4, 2021 at 14:34
  • would be better for you, if you try to use a linked list, and to remove the an item from the list would be much easier. you can't use free for single elements in the array ,free should free all the array. stackoverflow.com/questions/2946660/… Commented Apr 4, 2021 at 15:43

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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() .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.