From f34980c67bd57e82c514881960c788736d4a39a4 Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Sun, 17 Sep 2023 22:55:08 -0300 Subject: [PATCH] Add valgrind testing and fix valgrind errors --- .../.idea/runConfigurations/Assignment1.xml | 2 +- .../Assignment1_Valgrind.xml | 7 +++++++ .../.idea/runConfigurations/For_Testing.xml | 2 +- Assignment1/.idea/runConfigurations/Test.xml | 4 ++-- .../.idea/runConfigurations/Valgrind.xml | 19 +++++++++++++++++++ Assignment1/linked_list.c | 1 - Assignment1/linked_list.h | 4 ---- Assignment1/main.c | 4 ---- Assignment1/node.c | 8 ++------ Assignment1/node.h | 4 ---- Assignment1/tests/valgrind.sh | 8 ++++++++ 11 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 Assignment1/.idea/runConfigurations/Assignment1_Valgrind.xml create mode 100644 Assignment1/.idea/runConfigurations/Valgrind.xml create mode 100644 Assignment1/tests/valgrind.sh diff --git a/Assignment1/.idea/runConfigurations/Assignment1.xml b/Assignment1/.idea/runConfigurations/Assignment1.xml index 351a202..05461ab 100644 --- a/Assignment1/.idea/runConfigurations/Assignment1.xml +++ b/Assignment1/.idea/runConfigurations/Assignment1.xml @@ -1,5 +1,5 @@ - + diff --git a/Assignment1/.idea/runConfigurations/Assignment1_Valgrind.xml b/Assignment1/.idea/runConfigurations/Assignment1_Valgrind.xml new file mode 100644 index 0000000..5ccceef --- /dev/null +++ b/Assignment1/.idea/runConfigurations/Assignment1_Valgrind.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/Assignment1/.idea/runConfigurations/For_Testing.xml b/Assignment1/.idea/runConfigurations/For_Testing.xml index 58eb5d6..0cc5ee6 100644 --- a/Assignment1/.idea/runConfigurations/For_Testing.xml +++ b/Assignment1/.idea/runConfigurations/For_Testing.xml @@ -1,5 +1,5 @@ - + diff --git a/Assignment1/.idea/runConfigurations/Test.xml b/Assignment1/.idea/runConfigurations/Test.xml index 7b75734..70e19ff 100644 --- a/Assignment1/.idea/runConfigurations/Test.xml +++ b/Assignment1/.idea/runConfigurations/Test.xml @@ -1,6 +1,6 @@ - \ No newline at end of file diff --git a/Assignment1/.idea/runConfigurations/Valgrind.xml b/Assignment1/.idea/runConfigurations/Valgrind.xml new file mode 100644 index 0000000..5481c52 --- /dev/null +++ b/Assignment1/.idea/runConfigurations/Valgrind.xml @@ -0,0 +1,19 @@ + + + + \ No newline at end of file diff --git a/Assignment1/linked_list.c b/Assignment1/linked_list.c index c31ac01..d32355c 100644 --- a/Assignment1/linked_list.c +++ b/Assignment1/linked_list.c @@ -6,7 +6,6 @@ #define MAX_LEN 100 - Node *search(Node **head, char *data) { Node *current = *head; while (current != NULL) { diff --git a/Assignment1/linked_list.h b/Assignment1/linked_list.h index 003398b..1fbfe04 100644 --- a/Assignment1/linked_list.h +++ b/Assignment1/linked_list.h @@ -1,7 +1,3 @@ -// -// Created by Isaac on 9/15/2023. -// - #ifndef ASSIGNMENT1_LINKED_LIST_H #define ASSIGNMENT1_LINKED_LIST_H diff --git a/Assignment1/main.c b/Assignment1/main.c index c9ecc85..b4433df 100644 --- a/Assignment1/main.c +++ b/Assignment1/main.c @@ -1,12 +1,8 @@ -// -// Created by Isaac on 9/15/2023. -// #include #include #include #include "linked_list.h" - #define MAX_LEN 100 int main() { diff --git a/Assignment1/node.c b/Assignment1/node.c index b5b71d7..258c470 100644 --- a/Assignment1/node.c +++ b/Assignment1/node.c @@ -1,7 +1,3 @@ -// -// Created by Isaac on 9/15/2023. -// - #include #include #include @@ -16,7 +12,7 @@ Node *createNode(char *data) { exit(EXIT_FAILURE); } // Allocate memory for the data, and check if it was successful - node->data = calloc(strlen(data), sizeof(char)); + node->data = calloc(strlen(data) + 1, sizeof(char)); if (node->data == NULL) { printf("Error allocating memory for node data\n"); exit(EXIT_FAILURE); @@ -38,7 +34,7 @@ void destroyNode(Node *node) { void replaceData(Node *node, char *data) { // Free the data first, then allocate memory for the new data free(node->data); - node->data = calloc(strlen(data), sizeof(char)); + node->data = calloc(strlen(data) + 1, sizeof(char)); if (node->data == NULL) { printf("Error allocating memory for replacing data\n"); exit(EXIT_FAILURE); diff --git a/Assignment1/node.h b/Assignment1/node.h index 01655f3..e22fad5 100644 --- a/Assignment1/node.h +++ b/Assignment1/node.h @@ -1,7 +1,3 @@ -// -// Created by Isaac on 9/15/2023. -// - #ifndef ASSIGNMENT1_NODE_H #define ASSIGNMENT1_NODE_H typedef struct node { diff --git a/Assignment1/tests/valgrind.sh b/Assignment1/tests/valgrind.sh new file mode 100644 index 0000000..b49b8ce --- /dev/null +++ b/Assignment1/tests/valgrind.sh @@ -0,0 +1,8 @@ +#!/usr/bin/bash + +# Could make more complicated and more similar to test.sh, but only the most complicated test case is fine for now + +valgrind --leak-check=full \ + --show-leak-kinds=definite \ + --track-origins=yes \ + ./../build/Assignment1 < pdf2.txt \ No newline at end of file