Commit c668cf51 authored by Dominik Widhalm's avatar Dominik Widhalm
Browse files

Added error handling to malloc in tasks 6.02 to 6.10

parent 4d3a4d79
...@@ -55,6 +55,13 @@ double get_price (car_t *car); ...@@ -55,6 +55,13 @@ double get_price (car_t *car);
car_t* get_car (char* brand, int speed, int doors, feature abs) { car_t* get_car (char* brand, int speed, int doors, feature abs) {
/* Allocate memory for the new car object */ /* Allocate memory for the new car object */
car_t* new_car = (car_t*)malloc(sizeof(car_t)); car_t* new_car = (car_t*)malloc(sizeof(car_t));
/* Check if memory allocation was successful */
if (new_car == NULL) {
/* Return NULL to caller */
return NULL;
}
/* Set the properties accordingly */ /* Set the properties accordingly */
strcpy(new_car->brand,brand); strcpy(new_car->brand,brand);
new_car->speed = speed; new_car->speed = speed;
...@@ -148,11 +155,15 @@ int main (void) { ...@@ -148,11 +155,15 @@ int main (void) {
/* Get the corresponding car object */ /* Get the corresponding car object */
car = get_car(brand,speed,doors,abs); car = get_car(brand,speed,doors,abs);
/* Calculate the price for this car */ /* Check if car object was created */
price += get_price(car); if (car == NULL) {
printf("Could not create new car!\n");
/* Free the memory of the current car */ } else {
free(car); /* Calculate the price for this car */
price += get_price(car);
/* Free the memory of the current car */
free(car);
}
/* Flush stdin buffer */ /* Flush stdin buffer */
while (((ch = getchar()) != '\n') && (ch != EOF)); while (((ch = getchar()) != '\n') && (ch != EOF));
......
...@@ -52,6 +52,11 @@ void list_print (coord_cart* head, int index); ...@@ -52,6 +52,11 @@ void list_print (coord_cart* head, int index);
coord_cart* get_coord_cart (int x, int y) { coord_cart* get_coord_cart (int x, int y) {
/* Allocate memory for the new coordinate */ /* Allocate memory for the new coordinate */
coord_cart* new = (coord_cart*)malloc(sizeof(coord_cart)); coord_cart* new = (coord_cart*)malloc(sizeof(coord_cart));
/* Check if memory allocation was successful */
if (new == NULL) {
/* Return NULL to caller */
return NULL;
}
/* Set the coordinates accordingly */ /* Set the coordinates accordingly */
new->x = x; new->x = x;
new->y = y; new->y = y;
...@@ -203,8 +208,14 @@ int main (void) { ...@@ -203,8 +208,14 @@ int main (void) {
scanf("%d/%d",&x,&y); scanf("%d/%d",&x,&y);
/* Create coordinate element */ /* Create coordinate element */
new = get_coord_cart(x,y); new = get_coord_cart(x,y);
/* Add element to the list */ /* Check if a new coordinate could be created */
list_append(&head,new); if (new == NULL) {
/* Could not create new coordinate */
printf("Could not create new coordinate!\n");
} else {
/* Add element to the list */
list_append(&head,new);
}
break; break;
case 'p': case 'p':
/* Ask user to enter the coordinates (Polar) */ /* Ask user to enter the coordinates (Polar) */
......
...@@ -54,6 +54,14 @@ void list_add_person (person_t** head) { ...@@ -54,6 +54,14 @@ void list_add_person (person_t** head) {
/* Allocate memory for the new person */ /* Allocate memory for the new person */
person_t* new = (person_t*)malloc(sizeof(person_t)); person_t* new = (person_t*)malloc(sizeof(person_t));
/* Check if memory allocation was successful */
if (new == NULL) {
/* Memory allocation failed */
printf("Cannot allocate memory!\n");
/* Return to caller */
return;
}
/** Read in the person's properties **/ /** Read in the person's properties **/
/* Ask the user to input the persons first name */ /* Ask the user to input the persons first name */
printf("Please enter the person's first name: "); printf("Please enter the person's first name: ");
......
...@@ -64,10 +64,19 @@ validity_t check_date (int day, int month); ...@@ -64,10 +64,19 @@ validity_t check_date (int day, int month);
* @param month Month of the event * * @param month Month of the event *
******************************************************************************/ ******************************************************************************/
void add_event (event_t **head, char *name, int day, month_t month) { void add_event (event_t **head, char *name, int day, month_t month) {
/* Allocate memory for the new entry */
event_t* new = (event_t*)malloc(sizeof(event_t));
/* Temporary variable for the current element */ /* Temporary variable for the current element */
event_t *curr = *head; event_t *curr = *head;
/* Allocate memory for the new entry */
event_t* new = (event_t*)malloc(sizeof(event_t));
/* Check if memory allocation was successful */
if (new == NULL) {
/* Memory allocation failed */
printf("Cannot allocate memory!\n");
/* Return to caller */
return;
}
/* Pass the arguments to this new event element */ /* Pass the arguments to this new event element */
strcpy(new->name,name); strcpy(new->name,name);
new->d = day; new->d = day;
......
...@@ -79,6 +79,13 @@ song_t *songs_new (char *file) { ...@@ -79,6 +79,13 @@ song_t *songs_new (char *file) {
} }
/* Allocate memory for the new entry */ /* Allocate memory for the new entry */
song_t* new = (song_t*)malloc(sizeof(song_t)); song_t* new = (song_t*)malloc(sizeof(song_t));
/* Check if memory allocation was successful */
if (new == NULL) {
/* Return NULL to caller */
return NULL;
}
/* Temporary variable to store ID3v1 information (128 byte for raw ID3 data) */ /* Temporary variable to store ID3v1 information (128 byte for raw ID3 data) */
char id3[128]; char id3[128];
......
...@@ -68,14 +68,22 @@ int get_random_number (void) { ...@@ -68,14 +68,22 @@ int get_random_number (void) {
* @param head Pointer to pointer to the first list element * * @param head Pointer to pointer to the first list element *
******************************************************************************/ ******************************************************************************/
void list_add_element (element_t **head) { void list_add_element (element_t **head) {
/* Allocate memory for the new person */
element_t* new = (element_t*)malloc(sizeof(element_t));
/* Temporary pointer to iterate over the list */
element_t* curr = *head;
/* Temporary variable to mark if value is already available */ /* Temporary variable to mark if value is already available */
int free_value; int free_value;
/* Random value to be filled in new element */ /* Random value to be filled in new element */
int number; int number;
/* Temporary pointer to iterate over the list */
element_t* curr = *head;
/* Allocate memory for the new person */
element_t* new = (element_t*)malloc(sizeof(element_t));
/* Check if memory allocation was successful */
if (new == NULL) {
/* Memory allocation failed */
printf("Cannot allocate memory!\n");
/* Return to caller */
return;
}
/** Fill new element with information **/ /** Fill new element with information **/
/* Until a free number was returned ... */ /* Until a free number was returned ... */
......
...@@ -66,6 +66,11 @@ void list_print (line_t* head); ...@@ -66,6 +66,11 @@ void list_print (line_t* head);
line_t *line_new (char *text) { line_t *line_new (char *text) {
/* Allocate memory for the new entry */ /* Allocate memory for the new entry */
line_t* new = (line_t*)malloc(sizeof(line_t)); line_t* new = (line_t*)malloc(sizeof(line_t));
/* Check if memory allocation was successful */
if (new == NULL) {
/* Return NULL to caller */
return NULL;
}
/* Copy the given string */ /* Copy the given string */
strcpy(new->text,text); strcpy(new->text,text);
/* Set character counter initially to 0 */ /* Set character counter initially to 0 */
...@@ -173,8 +178,14 @@ readin_t list_read_textfile (line_t **head, char *file) { ...@@ -173,8 +178,14 @@ readin_t list_read_textfile (line_t **head, char *file) {
read[index] = '\0'; read[index] = '\0';
/* Create a new element with current line */ /* Create a new element with current line */
new = line_new(read); new = line_new(read);
/* Add new element to the list */ /* Check if new line could be added */
list_add_line(head,new); if (new == NULL) {
/* Could not create new line */
printf("Could not create new line!\n");
} else {
/* Add new element to the list */
list_add_line(head,new);
}
} }
/* No more content to be done */ /* No more content to be done */
break; break;
...@@ -186,8 +197,14 @@ readin_t list_read_textfile (line_t **head, char *file) { ...@@ -186,8 +197,14 @@ readin_t list_read_textfile (line_t **head, char *file) {
index = 0; index = 0;
/* Create a new element with current line */ /* Create a new element with current line */
new = line_new(read); new = line_new(read);
/* Add new element to the list */ /* Check if new line could be added */
list_add_line(head,new); if (new == NULL) {
/* Could not create new line */
printf("Could not create new line!\n");
} else {
/* Add new element to the list */
list_add_line(head,new);
}
/* Otherwise copy the current character to the temp string */ /* Otherwise copy the current character to the temp string */
} else { } else {
read[index++] = (char)tmp; read[index++] = (char)tmp;
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
/***** TYPEDEFS ***************************************************************/ /***** TYPEDEFS ***************************************************************/
/* Enumeration type for push & pop return value */ /* Enumeration type for push & pop return value */
typedef enum {OK, EMPTY, FULL} retval_t; typedef enum {OK, EMPTY, FULL, FAILED} retval_t;
/* Struct type for the single linked list */ /* Struct type for the single linked list */
typedef struct cmd { typedef struct cmd {
// Command (string) // Command (string)
...@@ -53,6 +53,7 @@ void stack_print (cmd_t *head); ...@@ -53,6 +53,7 @@ void stack_print (cmd_t *head);
* @param command Pointer to the desired command * * @param command Pointer to the desired command *
* @retval OK Pushing successful * * @retval OK Pushing successful *
* @retval FULL Pushing failed -- stack is full * * @retval FULL Pushing failed -- stack is full *
* @retval FAILED Pushing failed -- allocation failed *
******************************************************************************/ ******************************************************************************/
retval_t stack_push (cmd_t **head, char *command) { retval_t stack_push (cmd_t **head, char *command) {
/* Temporary pointer to the current stack element */ /* Temporary pointer to the current stack element */
...@@ -75,6 +76,11 @@ retval_t stack_push (cmd_t **head, char *command) { ...@@ -75,6 +76,11 @@ retval_t stack_push (cmd_t **head, char *command) {
/* Allocate memory for the new command */ /* Allocate memory for the new command */
curr = (cmd_t*)malloc(sizeof(cmd_t)); curr = (cmd_t*)malloc(sizeof(cmd_t));
/* Check if memory allocation was successful */
if (curr == NULL) {
/* Return FAILED to caller */
return FAILED;
}
/* Copy the command string */ /* Copy the command string */
strcpy(curr->command,command); strcpy(curr->command,command);
/* Set the pointers accordingly */ /* Set the pointers accordingly */
...@@ -146,6 +152,7 @@ int main (void) { ...@@ -146,6 +152,7 @@ int main (void) {
/*** Local Variables ***/ /*** Local Variables ***/
cmd_t *head = NULL; cmd_t *head = NULL;
cmd_t *next; cmd_t *next;
retval_t ret;
char select = 'x'; char select = 'x';
char command[STRING_MAX]; char command[STRING_MAX];
...@@ -170,12 +177,17 @@ int main (void) { ...@@ -170,12 +177,17 @@ int main (void) {
/* Read in the user's input */ /* Read in the user's input */
scanf("%s",command); scanf("%s",command);
/* (Try to) push the command on the stack */ /* (Try to) push the command on the stack */
if (stack_push(&head,command) == FULL) { ret = stack_push(&head,command);
/* Check if pushing was successful */
if (ret == OK) {
/* Command has been successfully pushed */
printf("\nPUSHED\n\n");
} else if (ret == FULL) {
/* Command cannot be pushed, stack is full */ /* Command cannot be pushed, stack is full */
printf("\nFAILED -- Stack is already full!\n\n"); printf("\nFAILED -- Stack is already full!\n\n");
} else { } else {
/* Command has been successfully pushed */ /* Command cannot be pushed, allocation error */
printf("\nPUSHED\n\n"); printf("\nFAILED -- Cannot allocate memory!\n\n");
} }
/** Finished inserting **/ /** Finished inserting **/
break; break;
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
/***** TYPEDEFS ***************************************************************/ /***** TYPEDEFS ***************************************************************/
/* Enumeration type for insert & remove return value */ /* Enumeration type for insert & remove return value */
typedef enum {OK, EMPTY, FULL, OLDREMOVED} retval_t; typedef enum {OK, EMPTY, FULL, OLDREMOVED, FAILED} retval_t;
/* Struct type for the single linked list */ /* Struct type for the single linked list */
typedef struct element { typedef struct element {
// ID // ID
...@@ -62,6 +62,7 @@ void queue_print (element_t *head); ...@@ -62,6 +62,7 @@ void queue_print (element_t *head);
* @retval OK Pushing successful * * @retval OK Pushing successful *
* @retval FULL Pushing failed -- stack is full * * @retval FULL Pushing failed -- stack is full *
* @retval OLDREMOVED Pushing successful, but old element removed * * @retval OLDREMOVED Pushing successful, but old element removed *
* @retval FAILED Pushing failed, Could not allocate memory *
******************************************************************************/ ******************************************************************************/
retval_t queue_insert (element_t **head, char *string, int priority) { retval_t queue_insert (element_t **head, char *string, int priority) {
/* Temporary pointer to the current queue element */ /* Temporary pointer to the current queue element */
...@@ -71,6 +72,13 @@ retval_t queue_insert (element_t **head, char *string, int priority) { ...@@ -71,6 +72,13 @@ retval_t queue_insert (element_t **head, char *string, int priority) {
/* Allocate memory for the new command */ /* Allocate memory for the new command */
element_t *new = (element_t*)malloc(sizeof(element_t)); element_t *new = (element_t*)malloc(sizeof(element_t));
/* Check if memory allocation was successful */
if (new == NULL) {
/* Return FAILED to caller */
return FAILED;
}
/* Copy the string */ /* Copy the string */
strcpy(new->string,string); strcpy(new->string,string);
/* Copy the priority */ /* Copy the priority */
...@@ -253,6 +261,9 @@ int main (void) { ...@@ -253,6 +261,9 @@ int main (void) {
if (ret == FULL) { if (ret == FULL) {
/* Cannot be added, queue is full */ /* Cannot be added, queue is full */
printf("\nFAILED -- QUEUE is already full!\n"); printf("\nFAILED -- QUEUE is already full!\n");
} else if (ret == FAILED) {
/* Cannot be added, could not allocate memory */
printf("\nFAILED -- Could not allocate memory!\n");
} else if (ret == OLDREMOVED) { } else if (ret == OLDREMOVED) {
/* Added, but an old element was removed */ /* Added, but an old element was removed */
printf("\nINSERTED -- But an old element was removed!\n"); printf("\nINSERTED -- But an old element was removed!\n");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment