aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-09-20 19:37:13 -0400
committerTucker Evans <tuckerevans24@gmail.com>2019-09-20 19:37:13 -0400
commit944512372b71c0a03639264896e791d80e3b0143 (patch)
tree2b407070d3ea6aae7a810c88a0dd9c129963a3a6
parentb0fa80e34881152350d3994eb1d28687d4d47d84 (diff)
Fix removes some string duplication
-rw-r--r--node.c37
-rw-r--r--pc.y5
2 files changed, 24 insertions, 18 deletions
diff --git a/node.c b/node.c
index 48826ca..df56c6f 100644
--- a/node.c
+++ b/node.c
@@ -12,7 +12,7 @@ char *str;
node *p = malloc(sizeof(node));
assert(p);
- p->name = strdup(str);
+ p->name = str;
p->next = NULL;
p->var_type = -1;
@@ -38,7 +38,7 @@ char *str;
return NULL;
}
-node* list_insert(root, str) /*TODO change to accept double pointer*/
+node* list_insert(root, str)
node *root;
char * str;
{
@@ -46,6 +46,23 @@ char * str;
p->next = root;
return p;
}
+void free_node(n)
+node *n;
+{
+ free(n->name);
+ n->name = NULL;
+
+ if (n->func_info)
+ free(n->func_info);
+ n->func_info = NULL;
+
+ if (n->array_info)
+ free(n->array_info);
+ n->array_info = NULL;
+
+ free(n);
+ n = NULL;
+}
void free_list(n)
node *n;
@@ -54,21 +71,7 @@ node *n;
for(tmp = n; tmp;) {
n = tmp->next;
-
- free(tmp->name);
- tmp->name = NULL;
-
- if (tmp->func_info)
- free(tmp->func_info);
- tmp->func_info = NULL;
-
- if (tmp->array_info)
- free(tmp->array_info);
- tmp->array_info = NULL;
-
- free(tmp);
- tmp = NULL;
-
+ free_node(tmp);
tmp = n;
}
}
diff --git a/pc.y b/pc.y
index ec78f22..a264286 100644
--- a/pc.y
+++ b/pc.y
@@ -2,6 +2,7 @@
#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
+#include <string.h>
#include "node.h"
#include "scope.h"
@@ -201,7 +202,9 @@ sub_prog_head
tmp->var_type = $5;
- cur_scope->ret_var = scope_insert(cur_scope, $2);
+ /* Need to duplicate ID.name so it is not freed with inner
+ * scope*/
+ cur_scope->ret_var = scope_insert(cur_scope, strdup($2));
cur_scope->ret_var->var_type = $5;
}
|PROC ID arguments ';'