Fix memory leaks
This commit is contained in:
parent
5392258a0d
commit
0afd640dcc
@ -14,9 +14,9 @@ void es_addsymbol(struct Extstring* string, char symbol) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int es_size(struct Extstring* string) {
|
int es_size(struct Extstring* string) {
|
||||||
int size;
|
int size = 0;
|
||||||
struct Extstring* ptr = string;
|
struct Extstring* ptr = string;
|
||||||
for (size = 0; ptr != NULL; size++) {
|
for (; ptr != NULL; size++) {
|
||||||
ptr = ptr->next;
|
ptr = ptr->next;
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
@ -37,19 +37,17 @@ void es_free(struct Extstring* string) {
|
|||||||
free(ptr);
|
free(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SString* es_tostring(struct Extstring* string) {
|
char* es_tostring(struct Extstring* string) {
|
||||||
struct Extstring* ptr = string;
|
struct Extstring* ptr = string;
|
||||||
int size = es_size(string) + 1;
|
int size = es_size(string) + 1;
|
||||||
struct SString* sstring = malloc(sizeof(struct SString));
|
char* cstring = malloc(size);
|
||||||
sstring->string = malloc(size);
|
|
||||||
sstring->size = size;
|
|
||||||
for (int i = 0; i != size; i++) {
|
for (int i = 0; i != size; i++) {
|
||||||
if (ptr != NULL) {
|
if (ptr != NULL) {
|
||||||
sstring->string[i] = ptr->symbol;
|
cstring[i] = ptr->symbol;
|
||||||
ptr = ptr->next;
|
ptr = ptr->next;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
sstring->string[i] = 0;
|
cstring[i] = 0;
|
||||||
}
|
}
|
||||||
return sstring;
|
return cstring;
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,7 @@ struct Extstring {
|
|||||||
struct Extstring* next;
|
struct Extstring* next;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SString {
|
|
||||||
int size;
|
|
||||||
char* string;
|
|
||||||
};
|
|
||||||
|
|
||||||
void es_addsymbol(struct Extstring* string, char symbol);
|
void es_addsymbol(struct Extstring* string, char symbol);
|
||||||
int es_size(struct Extstring* string);
|
int es_size(struct Extstring* string);
|
||||||
void es_free(struct Extstring* string);
|
void es_free(struct Extstring* string);
|
||||||
struct SString* es_tostring(struct Extstring* string);
|
char* es_tostring(struct Extstring* string);
|
||||||
|
@ -15,10 +15,13 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
file = fopen(argv[1],"r");
|
file = fopen(argv[1],"r");
|
||||||
struct SyntaxElement* code = parse(file);
|
struct SyntaxElement* code = parse(file);
|
||||||
|
fclose(file);
|
||||||
if (code == NULL) {
|
if (code == NULL) {
|
||||||
fprintf(stderr, "Error during parsing.\n");
|
fprintf(stderr, "Error during parsing.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
printtree(code,0);
|
printtree(code,0);
|
||||||
|
se_free(code);
|
||||||
|
code = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
25
src/parser.c
25
src/parser.c
@ -28,6 +28,27 @@ struct SyntaxElement* se_next(struct SyntaxElement* syntaxelement) {
|
|||||||
return syntaxelement->next;
|
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) {
|
void se_clean(struct SyntaxElement* syntaxtree) {
|
||||||
if (syntaxtree->next != NULL) {
|
if (syntaxtree->next != NULL) {
|
||||||
switch (syntaxtree->next->type) {
|
switch (syntaxtree->next->type) {
|
||||||
@ -80,7 +101,7 @@ struct SyntaxElement* parse(FILE* file) {
|
|||||||
}
|
}
|
||||||
instring = false;
|
instring = false;
|
||||||
syntaxtree->type = STRING;
|
syntaxtree->type = STRING;
|
||||||
char* sstring = es_tostring(string)->string;
|
char* sstring = es_tostring(string);
|
||||||
syntaxtree->content = sstring;
|
syntaxtree->content = sstring;
|
||||||
es_free(string);
|
es_free(string);
|
||||||
string = NULL;
|
string = NULL;
|
||||||
@ -99,7 +120,7 @@ struct SyntaxElement* parse(FILE* file) {
|
|||||||
if (isspace(symbol) || symbol == '(' || symbol == ')') {
|
if (isspace(symbol) || symbol == '(' || symbol == ')') {
|
||||||
intoken = false;
|
intoken = false;
|
||||||
syntaxtree->type = TOKEN;
|
syntaxtree->type = TOKEN;
|
||||||
char* sstring = es_tostring(token)->string;
|
char* sstring = es_tostring(token);
|
||||||
syntaxtree->content = sstring;
|
syntaxtree->content = sstring;
|
||||||
es_free(token);
|
es_free(token);
|
||||||
token = NULL;
|
token = NULL;
|
||||||
|
@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
#ifndef PARSER_H
|
#ifndef PARSER_H
|
||||||
#define 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 {
|
enum SyntaxElementType {
|
||||||
TOPTREE,
|
TOPTREE,
|
||||||
TREE,
|
TREE,
|
||||||
|
Loading…
Reference in New Issue
Block a user