From 3e6473a9e40b552b45d78425d58cf5921c966fe2 Mon Sep 17 00:00:00 2001 From: komiko44240BB Date: Sat, 26 Oct 2024 17:46:52 +0200 Subject: [PATCH] update and addition of delete functions --- objects.c | 28 ++++++++++++++++++++++++++++ objects.h | 2 +- perks.c | 35 +++++++++++++++++++++++++++++------ perks.h | 3 ++- readtraining.c | 25 +++++++++++++++++++++++++ readtraining.h | 1 + 6 files changed, 86 insertions(+), 8 deletions(-) diff --git a/objects.c b/objects.c index 16b74da..d7c233e 100644 --- a/objects.c +++ b/objects.c @@ -167,3 +167,31 @@ void addPerk(struct Perk* p, struct Object* o) { } printf("Perk could not be added, maximum perks reached.\n"); } + +/** + * @brief Deletes all Objects in a linked list contained within a Slots structure. + * + * This function traverses the linked list of Objects pointed to by the head + * of the provided Slots structure. It frees each Object in the list and + * finally frees the Slots structure itself. + * + * @param slots A pointer to the Slots structure that contains the linked list + * of Objects. If NULL, the function returns immediately. + */ +void deleteSlots(struct Slots* slots) { + if (slots == NULL) { + return; // Nothing to delete + } + + struct Object* current = slots->head; // Pointer to the current Object in the list + struct Object* next; // Pointer to the next Object to be deleted + + // Iterate through the linked list of Objects and free each one + while (current != NULL) { + next = current->next; // Store the next object to process + deleteObject(current); // Free the current object from memory + current = next; // Move to the next object in the list + } + + free(slots); // Free the Slots structure itself to avoid memory leaks +} diff --git a/objects.h b/objects.h index b033498..3f428f7 100644 --- a/objects.h +++ b/objects.h @@ -48,5 +48,5 @@ void deleteObject(struct Object* o); void addPerk(struct Perk* p, struct Object* o); struct Slots* createSlots(); - +void deleteSlots(struct Slots* slots); #endif diff --git a/perks.c b/perks.c index 70eaa97..4b67fb0 100644 --- a/perks.c +++ b/perks.c @@ -491,10 +491,33 @@ char* perk_to_string(struct Perk* p) { * * @param p Perk struct containing the details to be displayed. */ -void displayPerk(struct Perk p) { - printf("Is Event: %s\n", p.is_event ? "True" : "False"); - printf("First Boost Value: %.2f\n", p.first_boost_value); - printf("First Boost Type: %s\n", p.first_boost_type); - printf("Second Boost Value: %.2f\n", p.second_boost_value); - printf("Second Boost Type: %s\n", p.second_boost_type); +void displayPerk(struct Perk* p) { + printf("Is Event: %s\n", p->is_event ? "True" : "False"); + printf("First Boost Value: %.2f\n", p->first_boost_value); + printf("First Boost Type: %s\n", p->first_boost_type); + printf("Second Boost Value: %.2f\n", p->second_boost_value); + printf("Second Boost Type: %s\n", p->second_boost_type); } + +/** + * Frees memory allocated for a Perk struct and its members. + * + * @param p Pointer to the Perk struct to be deleted. If p is NULL, + * the function does nothing. Otherwise, it frees memory + * for each allocated member before freeing the Perk itself. + */ +void deletePerk(struct Perk* p) { + if (p == NULL) { // Check if the pointer is NULL to avoid unnecessary operations. + return; + } + if (p->first_boost_type != NULL) { // Free first_boost_type if it has been allocated. + free(p->first_boost_type); + p->first_boost_type = NULL; // Set pointer to NULL to avoid dangling pointers. + } + if (p->second_boost_type != NULL) { // Free second_boost_type if it has been allocated. + free(p->second_boost_type); + p->second_boost_type = NULL; // Set pointer to NULL to avoid dangling pointers. + } + free(p); // Free the Perk struct itself. +} + diff --git a/perks.h b/perks.h index 9a6a254..e62e25f 100644 --- a/perks.h +++ b/perks.h @@ -20,5 +20,6 @@ struct Perk* displayAndChooseAvailablePerks(char** perk_list, bool is_event, str bool perk_already_fited(bool is_event, struct Object* o, char* line); char* perk_to_string(struct Perk* p); void updateEvent(struct Perk* p,bool is_event); -void displayPerk(struct Perk p ); +void displayPerk(struct Perk* p ); +void deletePerk(struct Perk* p); #endif diff --git a/readtraining.c b/readtraining.c index 7c868d8..239ebe8 100644 --- a/readtraining.c +++ b/readtraining.c @@ -151,4 +151,29 @@ struct Training* readTraining(char* object_name) { } fclose(file); // Close the file once training data is successfully read return t; // Return the populated Training struct +} + +/** + * @brief Deletes a Training struct and frees associated memory. + * + * This function checks if the given Training struct pointer is not NULL, + * then proceeds to free the memory allocated for its members and the struct itself. + * It is important to ensure that each member is freed only if it was previously allocated. + * + * @param t A pointer to the Training struct to be deleted. + */ +void deleteTraining(struct Training* t) { + // Check if the Training struct pointer is not NULL + if (t != NULL) { + // Free the memory allocated for the 'type' array, if it exists + if (t->type != NULL) { + free(t->type); // Free the type array + } + // Free the memory allocated for the 'value' array, if it exists + if (t->value != NULL) { + free(t->value); // Free the value array + } + // Free the memory allocated for the Training struct itself + free(t); // Free the Training struct + } } \ No newline at end of file diff --git a/readtraining.h b/readtraining.h index a54a8e5..c99dbb9 100644 --- a/readtraining.h +++ b/readtraining.h @@ -8,4 +8,5 @@ struct Training { }; struct Training* createTraining(unsigned int training_lvl); struct Training* readTraining(char* object_name); +void deleteTraining(struct Training* t); #endif