From a669e59abcc219c0ceca293c48370b0e4298d881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20LACROIX?= Date: Thu, 6 Apr 2023 10:58:46 +0200 Subject: [PATCH] Added reallocation (untested) --- table.c | 29 +++++++++++++++++++++++++++++ table.h | 14 ++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/table.c b/table.c index 76fa009..c01cb9b 100644 --- a/table.c +++ b/table.c @@ -1,2 +1,31 @@ #include +#include + +/*At the start of the execution : the whole array is empty*/ +symbolTable = malloc(sizeof(symbol) * START_TABLE_SIZE) +currentIndex = 0; +maxIndex = START_TABLE_SIZE; + +/*Checks for the length of the array and reallocates if necessary*/ +void checkArraySanity(){ + if (currentIndex == maxIndex){ + reallocateArray(maxIndex * 2); + } else { + if (currentIndex < maxIndex / 2){ + reallocateArray(maxIndex / 2); + } + } +} + +void reallocateArray(int size){ + Symbol* newSymbolTable = malloc(sizeof (symbol) * size); // we double the size of the array + Symbol* oldSymbolTable = symbolTable; // we double the size of the array + free(oldSymbolTable); + + for(int i = 0; i < currentIndex; i++){ + newSymbolTable[i] = oldSymbolTable[i]; + } + + symbolTable = newSymbolTable; +} \ No newline at end of file diff --git a/table.h b/table.h index bb4d3be..acd9170 100644 --- a/table.h +++ b/table.h @@ -1,6 +1,17 @@ +#ifndef TABLE_H +#define TABLE_H + #include #include +/*defined constants*/ +#define START_TABLE_SIZE 128; + +/*indexes in the array*/ +static int currentIndex; +static int maxIndex; +static symbol* symbolTable; + enum enumVarType {INT, FLOAT, CHAR, STRING}; struct symbol { @@ -11,4 +22,7 @@ struct symbol { int deep; }; +void reallocateArray(int size); +void checkArraySanity(); +#endif