aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2022-12-31 21:00:55 +0200
committerIgor Pashev <pashev.igor@gmail.com>2022-12-31 21:25:00 +0200
commit9cd4f96eb320b59313fc7f83db56d63aa9b82891 (patch)
treea279fb886b617394da69d7b0d0e9abaf82243d06
parentde56e79dcb93c501add0c31ac9679eeb2ef80642 (diff)
downloadmendeleev-9cd4f96eb320b59313fc7f83db56d63aa9b82891.tar.gz
C: reuse formula
-rw-r--r--mendeleev.c26
1 files changed, 19 insertions, 7 deletions
diff --git a/mendeleev.c b/mendeleev.c
index 6436771..cad0308 100644
--- a/mendeleev.c
+++ b/mendeleev.c
@@ -155,6 +155,9 @@ print_plain (const element_t * current, size_t *formula, size_t n)
int
main (int argc, const char *argv[])
{
+ size_t *formula = NULL;
+ int rc = EXIT_SUCCESS;
+
for (int i = 1; i < argc; i++)
{
const char *word = argv[i];
@@ -164,18 +167,27 @@ main (int argc, const char *argv[])
if (!len)
continue;
+ size_t *f = (size_t *) realloc (formula, len * sizeof (size_t));
+ if (!f)
+ {
+ rc = EXIT_FAILURE;
+ break;
+ }
+
+ formula = f;
+
element_t *root = explode (word);
if (!root)
- return EXIT_FAILURE;
-
- size_t *formula = (size_t *) malloc (len * sizeof (size_t));
- if (!formula)
- return EXIT_FAILURE;
+ {
+ rc = EXIT_FAILURE;
+ break;
+ }
print_plain (root, formula, 0);
- free (formula);
free_elements (root);
}
- return EXIT_SUCCESS;
+ if (formula)
+ free (formula);
+ return rc;
}