hello sir, really good topic.
i have a question regarding the example on deep and shallow copy though,
struct person {
char* name;
};
void get_person(struct person* p) {
char new_name[20]; // on stack, gets freed when function returned
printf("input new name for person:");
scanf("%s", &new_name);
p->name = new_name;
printf("address of new_name = %p\n", &new_name[0]);
}
void eg_test_copy2(void) {
struct person p = {"alex"};
get_person(&p);
printf("p name = %s\n", p.name);
char dummy[20] = { 0 };
printf("address of dummy = %p\n", &dummy[0]);
printf("p name = %s\n", p.name);
}
if i call function `eg_test_copy2`, i noticed that the memory allocated for `new_name` never gets cleared,
the output will be
input new name for person:alex
address of new_name = 000000000061FD90
p name = alex
address of dummy = 000000000061FDE0
p name = alex
and the address for `dummy` actually starts after the address of new_name + 4 bytes x 20, and i can still access the old name, is this weired