Tengo cuatro capas diferentes de estructura anidadas. El código es el siguiente:
typedef struct System system;
typedef struct College college;
typedef struct Student student;
typedef struct Family family;
#define MAX_COLLEGES 10
#define MAX_NAME_LEN 32
#define MAX_STUDENTS 10
struct System {
college *Colleges[MAX_COLLEGES];
};
struct College {
char name[MAX_NAME_LEN];
student *Students[MAX_STUDENTS];
};
struct Student {
char name[MAX_NAME_LEN];
int id;
family *fam; //was typo familiy
};
struct Family {
char fatherName[MAX_NAME_LEN];
char motherName[MAX_NAME_LEN];
};
Y asigné memoria a todos ellos (no estoy seguro de si los asigné todos correctamente), de la siguiente manera:
system *collegeSys = malloc(sizeof(system));
college *colleges = malloc(sizeof(college));
student *students = malloc(sizeof(student));
family *fam = malloc(sizeof(family));
// then the following is initialization
...
...
...
Ahora, necesito eliminar la collegeSys
estructura y cualquier cosa asociada con ella. Entonces, no sé si puedo liberar la primera collegeSys
estructura sin liberar ninguna otra estructura, como esta:
free(collegeSys);
O para "eliminar cualquier cosa asociada con él", tengo que liberar todo de abajo hacia arriba, así:
free(fam);
free(students);
free(colleges);
free(collegeSys);
O con ese fin, incluso tengo que liberar todo lo que esté incluido dentro de cada estructura y liberarlo de abajo hacia arriba, así:
free (fam -> fatherName);
free (fam -> motherName);
free (fam);
free (students -> name);
free (students -> id);
free (students -> fam);
free (students)
.
. till
.
free (collegeSys -> colleges);
free (collegeSys);
¿Cuál es la forma correcta y segura de liberar la memoria? ¿O ninguno de ellos lo es?