Fix memory leaks

This commit is contained in:
bʰedoh₂ swé 2024-06-03 23:02:30 +05:00
parent 5392258a0d
commit 0afd640dcc
5 changed files with 39 additions and 17 deletions

View File

@ -14,9 +14,9 @@ void es_addsymbol(struct Extstring* string, char symbol) {
}
int es_size(struct Extstring* string) {
int size;
int size = 0;
struct Extstring* ptr = string;
for (size = 0; ptr != NULL; size++) {
for (; ptr != NULL; size++) {
ptr = ptr->next;
}
return size;
@ -37,19 +37,17 @@ void es_free(struct Extstring* string) {
free(ptr);
}
struct SString* es_tostring(struct Extstring* string) {
char* es_tostring(struct Extstring* string) {
struct Extstring* ptr = string;
int size = es_size(string) + 1;
struct SString* sstring = malloc(sizeof(struct SString));
sstring->string = malloc(size);
sstring->size = size;
char* cstring = malloc(size);
for (int i = 0; i != size; i++) {
if (ptr != NULL) {
sstring->string[i] = ptr->symbol;
cstring[i] = ptr->symbol;
ptr = ptr->next;
}
else
sstring->string[i] = 0;
cstring[i] = 0;
}
return sstring;
return cstring;
}

View File

@ -3,12 +3,7 @@ struct Extstring {
struct Extstring* next;
};
struct SString {
int size;
char* string;
};
void es_addsymbol(struct Extstring* string, char symbol);
int es_size(struct Extstring* string);
void es_free(struct Extstring* string);
struct SString* es_tostring(struct Extstring* string);
char* es_tostring(struct Extstring* string);

View File

@ -15,10 +15,13 @@ int main(int argc, char *argv[]) {
}
file = fopen(argv[1],"r");
struct SyntaxElement* code = parse(file);
fclose(file);
if (code == NULL) {
fprintf(stderr, "Error during parsing.\n");
return 1;
}
printtree(code,0);
se_free(code);
code = NULL;
return 0;
}

View File

@ -28,6 +28,27 @@ struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement) {
return syntaxelement->next;
}
void se_free(struct SyntaxElement* syntaxtree) {
switch (syntaxtree->type) {
case NONE:
free(syntaxtree);
return;
case TREE:
case TOPTREE:
if (syntaxtree->content != NULL) {
se_free((struct SyntaxElement*)syntaxtree->content);
}
break;
default:
free(syntaxtree->content);
break;
}
if (syntaxtree->next != NULL) {
se_free(syntaxtree->next);
}
free(syntaxtree);
}
void se_clean(struct SyntaxElement* syntaxtree) {
if (syntaxtree->next != NULL) {
switch (syntaxtree->next->type) {
@ -80,7 +101,7 @@ struct SyntaxElement* parse(FILE* file) {
}
instring = false;
syntaxtree->type = STRING;
char* sstring = es_tostring(string)->string;
char* sstring = es_tostring(string);
syntaxtree->content = sstring;
es_free(string);
string = NULL;
@ -99,7 +120,7 @@ struct SyntaxElement* parse(FILE* file) {
if (isspace(symbol) || symbol == '(' || symbol == ')') {
intoken = false;
syntaxtree->type = TOKEN;
char* sstring = es_tostring(token)->string;
char* sstring = es_tostring(token);
syntaxtree->content = sstring;
es_free(token);
token = NULL;

View File

@ -2,6 +2,11 @@
#ifndef PARSER_H
#define PARSER_H
struct SyntaxElement* se_init(void);
struct SyntaxElement* se_bottom(struct SyntaxElement* syntaxelement);
struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement);
void se_free(struct SyntaxElement* syntaxtree);
enum SyntaxElementType {
TOPTREE,
TREE,