Added reallocation (untested)
This commit is contained in:
parent
fbb27f6d31
commit
a669e59abc
2 changed files with 43 additions and 0 deletions
29
table.c
29
table.c
|
@ -1,2 +1,31 @@
|
|||
#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
14
table.h
|
@ -1,6 +1,17 @@
|
|||
#ifndef TABLE_H
|
||||
#define TABLE_H
|
||||
|
||||
#include <stdio.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};
|
||||
|
||||
struct symbol {
|
||||
|
@ -11,4 +22,7 @@ struct symbol {
|
|||
int deep;
|
||||
};
|
||||
|
||||
void reallocateArray(int size);
|
||||
void checkArraySanity();
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue