There is a weird phenomena that I am currently encountering in C's dynamic memory deallocating function, free(). In short, I've got this struct:
struct person {
char* name;
char* address;
int days[6]; //no work on sundays
};
And I've got a container struct for these structs:
struct person* people;
Now, I am aware of the fact that since I am allocating the person's name and address dynamically via malloc(), I am going to need to free() them accordingly, before freeing the people struct. The weird encountered behaviour happens when I free both the name and the address.If I only free one of the two, I get garbage printed out when dereferencing the (already dangling) pointer. But, if I free both, then the person's address won't get cleared, only the name. Here's my simple code debugging snippet:
printf("%s", people[person_counter].name);
printf("%s", people[7].name);
free(people[person_counter].name);
printf("%s", people[person_counter].name);
printf("%s", people[7].name);
//people[person_counter].name = NULL;
printf("%s", people[person_counter].address);
printf("%s", people[7].address);
free(people[person_counter].address);
printf("%s", people[person_counter].address);
printf("%s", people[7].address);
For debugging purposes I hardcoded the exact position of the to-be-freed person's data in the array. What happens, is that the first two lines print out the same name, after freeing they print out garbage. The address printing remains the same at all 4 places. What's more peculiar is that if I comment out the first free, the address will be printed the first two times, and it is going to be cleared afterwards. I understood that you cannot guarantee that the contents of the memory address after freeing won't be "the same as before", but here clearly the freeing process does not take place. As I couldn't really find any relevant info to this, help would be much appreciated.
EDIT: I was hoping that this would solve my little bit more complicated issue, but since it does not:
What I am doing is removing workers from my container struct and the simple logic that is used is that if they are not the last worker, the last worker's name and address (also working days) gets copied on the worker to be deleted and I am always freeing the last worker's data. I also noticed, that when I free both the name and both the address, the person's address stays the same, but the very next person's name will be garbage. This can be seen when I am printing out my list of workers. Is this indeterministic too, or how does another worker's name get cleared when I am sure I free only once, and then the name and the address (as desired) and not two names.
Example Worker1
add1
1
Example Worker2
add2
2
Example Worker8
add8
4
(null)add4
1
Example Worker5
add5
2
Example Worker6
add6
1
Example Worker7
add7
6
Output after I delete Example Worker3 and Example Worker8 gets copied to its place.