Skip to content

Commit

Permalink
Return NULL instead of ending the program
Browse files Browse the repository at this point in the history
We should return NULL instead of exiting the program. It is a bad
practice to exit the program. We should return a NULL pointer and let
the client program decide to what to do.
  • Loading branch information
StefanBossbaly committed May 18, 2012
1 parent 6003d33 commit 2c0592a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
12 changes: 4 additions & 8 deletions linkedlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
static struct node *linkedlist_alloc_node(struct node *next, void *data, size_t size) {
struct node *node = malloc(sizeof(struct node));

if (node == NULL) {
printf("Error allocating memory");
exit(1);
}
if (node == NULL)
return NULL;

node->next = next;

Expand All @@ -28,10 +26,8 @@ static inline void linkedlist_dealloc_node(struct node *node) {
struct list *linkedlist_alloc_list() {
struct list *list = malloc(sizeof(struct list));

if (list == NULL) {
printf("Error allocating memory");
exit(1);
}
if (list == NULL)
return NULL;

list->head = list->tail = linkedlist_alloc_node(NULL, NULL, 0);

Expand Down
16 changes: 12 additions & 4 deletions vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
static void vector_ensure_capacity(struct vector *vector) {
if ((vector->length + 1) == vector->capacity) {
vector->elements = (struct element **) realloc(vector->elements, sizeof(struct element) * 2 * vector->capacity);
if (vector->elements == NULL)
return;

vector->capacity *= 2;
}
}

struct element *element_alloc(void *data, size_t size) {
struct element *element = (struct element *) malloc(sizeof(struct element));

if (element == NULL) {
printf("Error allocating memory");
exit(1);
}
if (element == NULL)
return NULL;

if (data != NULL) {
element->data = malloc(size);
Expand All @@ -36,7 +37,14 @@ struct vector *vector_alloc() {
struct vector *vector_alloc_with_size(size_t size) {
struct vector *vector = (struct vector *) malloc(sizeof(struct vector));

if (vector == NULL)
return NULL;

vector->elements = (struct element **) malloc(size * sizeof(struct element));

if (vector->elements == NULL)
return NULL;

vector->capacity = size;
vector->length = 0;

Expand Down

0 comments on commit 2c0592a

Please sign in to comment.