diff --git a/CircularBuffer/CircularBuffer.c b/CircularBuffer/CircularBuffer.c index 5db54b1..9d6aa99 100644 --- a/CircularBuffer/CircularBuffer.c +++ b/CircularBuffer/CircularBuffer.c @@ -10,8 +10,24 @@ CircularBuffer * circularbuffer_initialize(size_t size, void * val){ size_t i; CircularBuffer * cb = (CircularBuffer *)malloc( sizeof(CircularBuffer) ); + + if (cb == NULL) { + + // any other implementation may be added here. + + printf("\nERROR: Insufficient memory. Terminating..."); + exit(EXIT_FAILURE); + } cb->buffer = (Item *)calloc(size, sizeof(Item)); + + if (cb->buffer == NULL) { + + // any other implementation may be added here. + + printf("\nERROR: Insufficient memory. Terminating..."); + exit(EXIT_FAILURE); + } for(i=0; ibuffer[i].data = val; diff --git a/HashTable/HashTable.c b/HashTable/HashTable.c index 0a6ceac..4ffda85 100644 --- a/HashTable/HashTable.c +++ b/HashTable/HashTable.c @@ -17,6 +17,11 @@ HashTable * hashtable_initialize(size_t size, size_t mc, size_t gf, hash_function fn, compare_equal efn){ size_t i; HashTable * ht = (HashTable *) malloc( sizeof(HashTable) ); + + if (ht == NULL) { + printf("\nERROR: Cannot create hash table. Terminating..."); + exit(EXIT_FAILURE); // well... any other implementation could be added, instead of this. + } ht->hf = fn; ht->eq = efn; @@ -25,6 +30,11 @@ HashTable * hashtable_initialize(size_t size, size_t mc, size_t gf, hash_functio ht->growthFactor = gf; ht->table = (Item **) malloc(size * sizeof( Item * )); + + if (ht->table == NULL) { + printf("\nERROR: Cannot create hash table. Terminating..."); + exit(EXIT_FAILURE); // well... any other implementation could be added, instead of this. + } for(i=0; itable[i] = 0; @@ -49,8 +59,19 @@ void * hashtable_get(HashTable * ht, void * key){ } int hashtable_destroy(HashTable * ht){ + + // Avoiding runtime errors. Return a successful destroy when the + // hashtable has not been initialized using the 'hashtable_initialize' + // function. + + if (ht == NULL) + return 1; + + // From here we know the hashtable has been properly initialized so, + // we continue computations. + size_t i; - + for(i=0; isize; i++){ free(ht->table[i]); } @@ -79,8 +100,8 @@ int hashtable_resize(HashTable * ht, size_t size){ } free(ht->table); - ht->size = newht->size; - ht->table = newht->table; + newht->size = ht->size; + newht->table = ht->table; return(1); } @@ -102,6 +123,13 @@ int hashtable_add(HashTable * ht, void * key, void * value){ } next = (Item *)malloc( sizeof(Item) ); + + // Making sure that memory was properly allocated for 'next'. + if (next == NULL) { + printf("\nERROR: Insufficient memory. Terminating...."); + exit(EXIT_FAILURE); // some other implementation could be used here. + } + next->key = key; next->value = value; next->next = ht->table[ hash % ht->size ]; diff --git a/List/ListItem.c b/List/ListItem.c index d289be5..28045c8 100644 --- a/List/ListItem.c +++ b/List/ListItem.c @@ -12,6 +12,13 @@ */ ListItem_t * list_item_initialize(){ ListItem_t * li = (ListItem_t *) malloc( sizeof(ListItem_t) ); + + // Making sure there was enough memory for a 'ListItem_t' type 'li'. + if (li == NULL) { + printf("\nERROR: Insufficient memory. Terminating..."); + exit(EXIT_FAILURE); + } + li->next = 0; return(li); diff --git a/Queue/Queue.c b/Queue/Queue.c index fe17028..a7a6373 100644 --- a/Queue/Queue.c +++ b/Queue/Queue.c @@ -20,11 +20,19 @@ int queue_push(Queue_t * q, void * d){ if(q->size == 0){ q->head = (QueueItem_t *) malloc( sizeof(QueueItem_t) ); + if (q->head == NULL) { + printf("\nERROR: Insufficient memory. Terminating..."); + exit(EXIT_FAILURE); + } q->head->next = 0; q->head->data = d; }else{ tmp = q->head; q->head = (QueueItem_t *) malloc( sizeof(QueueItem_t) ); + if (q->head == NULL) { + printf("\nERROR: Insufficient memory. Terminating..."); + exit(EXIT_FAILURE); + } q->head->data = d; q->head->next = tmp; }