about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDaniel Santos <dacs.git@brilhante.top>2022-03-12 13:21:39 +0000
committerDaniel Santos <dacs.git@brilhante.top>2022-03-12 13:21:39 +0000
commit90c869857b55782d4d6b21434a58da4d2f9e6be3 (patch)
treeb187c67c5f71d66a8b5bf800f752ac1c14948fe8
parent06bb8994d4675495e3fbd660378ec370337c884d (diff)
downloadnumericx-c-90c869857b55782d4d6b21434a58da4d2f9e6be3.tar.gz
free memory
 * add mem rule to Makefile
 * add free_numeral()
 * free all memory allocations

Signed-off-by: Daniel Santos <dacs.git@brilhante.top>
-rw-r--r--Makefile3
-rw-r--r--numericx.c28
2 files changed, 30 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 4943dd3..d923716 100644
--- a/Makefile
+++ b/Makefile
@@ -55,3 +55,6 @@ clean:
 run: decimal-earth
 	./decimal-earth 21
 
+mem: decimal-earth
+	valgrind --leak-check=full --show-leak-kinds=all -s decimal-earth 999
+
diff --git a/numericx.c b/numericx.c
index d12fc87..2870b13 100644
--- a/numericx.c
+++ b/numericx.c
@@ -177,7 +177,8 @@ reverse_string(char* string)
 	}
 }
 
-bool is_valid_number(char* numeral_system, char *number)
+bool
+is_valid_number(char* numeral_system, char *number)
 {
 	/*
 	if( *number == '-' || *number == '+' )
@@ -193,6 +194,19 @@ bool is_valid_number(char* numeral_system, char *number)
 	return true;
 }
 
+void
+free_numeral(numeral_ptr* numeral)
+{
+	numeral_ptr* tmp = NULL;
+
+	while( !(numeral == NULL) )
+	{
+		tmp = numeral->next;
+		free(numeral);
+		numeral = tmp;
+	}
+}
+
 int
 main(int argc, char* argv[])
 {
@@ -218,6 +232,8 @@ main(int argc, char* argv[])
 	/* Check if number belongs to it's numerical system */
 	if( !is_valid_number(from, number) )
 	{
+		free(from);
+		free(to);
 		fprintf(stderr, "error: %s.\n", strerror(EDOM));
 		exit(EDOM);
 	}
@@ -252,6 +268,10 @@ main(int argc, char* argv[])
 		{
 			if( strlen(number) == 1 && *number == *from_first )
 			{
+				free(from);
+				free(to);
+				free_numeral(counting);
+				free_numeral(result);
 				fprintf(stderr, "error: unrepresentable void number\n");
 				exit(EXIT_FAILURE);
 			}
@@ -295,5 +315,11 @@ main(int argc, char* argv[])
 	print_numeral(result);
 	printf("\n");
 
+	/* free memory */
+	free(from);
+	free(to);
+	free_numeral(counting);
+	free_numeral(result);
+
 	return EXIT_SUCCESS;
 }