Added reallocation (untested)

This commit is contained in:
Raphaël LACROIX 2023-04-06 10:58:46 +02:00
parent fbb27f6d31
commit a669e59abc
2 changed files with 43 additions and 0 deletions

29
table.c
View file

@ -1,2 +1,31 @@
#include <table.h> #include <table.h>
#include <stdlib.h>
/*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;
}

14
table.h
View file

@ -1,6 +1,17 @@
#ifndef TABLE_H
#define TABLE_H
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
/*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}; enum enumVarType {INT, FLOAT, CHAR, STRING};
struct symbol { struct symbol {
@ -11,4 +22,7 @@ struct symbol {
int deep; int deep;
}; };
void reallocateArray(int size);
void checkArraySanity();
#endif