Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
master-embedded-systems
c-exercise-solutions public
Commits
c668cf51
Commit
c668cf51
authored
Aug 28, 2017
by
Dominik Widhalm
Browse files
Added error handling to malloc in tasks 6.02 to 6.10
parent
4d3a4d79
Changes
9
Hide whitespace changes
Inline
Side-by-side
ch_6/task_02/main.c
View file @
c668cf51
...
...
@@ -55,6 +55,13 @@ double get_price (car_t *car);
car_t
*
get_car
(
char
*
brand
,
int
speed
,
int
doors
,
feature
abs
)
{
/* Allocate memory for the new car object */
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 */
strcpy
(
new_car
->
brand
,
brand
);
new_car
->
speed
=
speed
;
...
...
@@ -148,11 +155,15 @@ int main (void) {
/* Get the corresponding car object */
car
=
get_car
(
brand
,
speed
,
doors
,
abs
);
/* Calculate the price for this car */
price
+=
get_price
(
car
);
/* Free the memory of the current car */
free
(
car
);
/* Check if car object was created */
if
(
car
==
NULL
)
{
printf
(
"Could not create new car!
\n
"
);
}
else
{
/* Calculate the price for this car */
price
+=
get_price
(
car
);
/* Free the memory of the current car */
free
(
car
);
}
/* Flush stdin buffer */
while
(((
ch
=
getchar
())
!=
'\n'
)
&&
(
ch
!=
EOF
));
...
...
ch_6/task_03/main.c
View file @
c668cf51
...
...
@@ -52,6 +52,11 @@ void list_print (coord_cart* head, int index);
coord_cart
*
get_coord_cart
(
int
x
,
int
y
)
{
/* Allocate memory for the new coordinate */
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 */
new
->
x
=
x
;
new
->
y
=
y
;
...
...
@@ -203,8 +208,14 @@ int main (void) {
scanf
(
"%d/%d"
,
&
x
,
&
y
);
/* Create coordinate element */
new
=
get_coord_cart
(
x
,
y
);
/* Add element to the list */
list_append
(
&
head
,
new
);
/* Check if a new coordinate could be created */
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
;
case
'p'
:
/* Ask user to enter the coordinates (Polar) */
...
...
ch_6/task_04/main.c
View file @
c668cf51
...
...
@@ -54,6 +54,14 @@ void list_add_person (person_t** head) {
/* Allocate memory for the new person */
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 **/
/* Ask the user to input the persons first name */
printf
(
"Please enter the person's first name: "
);
...
...
ch_6/task_05/main.c
View file @
c668cf51
...
...
@@ -64,10 +64,19 @@ validity_t check_date (int day, int month);
* @param month Month of the event *
******************************************************************************/
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 */
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 */
strcpy
(
new
->
name
,
name
);
new
->
d
=
day
;
...
...
ch_6/task_06/main.c
View file @
c668cf51
...
...
@@ -79,6 +79,13 @@ song_t *songs_new (char *file) {
}
/* Allocate memory for the new entry */
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) */
char
id3
[
128
];
...
...
ch_6/task_07/main.c
View file @
c668cf51
...
...
@@ -68,14 +68,22 @@ int get_random_number (void) {
* @param head Pointer to pointer to the first list element *
******************************************************************************/
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 */
int
free_value
;
/* Random value to be filled in new element */
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 **/
/* Until a free number was returned ... */
...
...
ch_6/task_08/main.c
View file @
c668cf51
...
...
@@ -66,6 +66,11 @@ void list_print (line_t* head);
line_t
*
line_new
(
char
*
text
)
{
/* Allocate memory for the new entry */
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 */
strcpy
(
new
->
text
,
text
);
/* Set character counter initially to 0 */
...
...
@@ -173,8 +178,14 @@ readin_t list_read_textfile (line_t **head, char *file) {
read
[
index
]
=
'\0'
;
/* Create a new element with current line */
new
=
line_new
(
read
);
/* Add new element to the list */
list_add_line
(
head
,
new
);
/* Check if new line could be added */
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 */
break
;
...
...
@@ -186,8 +197,14 @@ readin_t list_read_textfile (line_t **head, char *file) {
index
=
0
;
/* Create a new element with current line */
new
=
line_new
(
read
);
/* Add new element to the list */
list_add_line
(
head
,
new
);
/* Check if new line could be added */
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 */
}
else
{
read
[
index
++
]
=
(
char
)
tmp
;
...
...
ch_6/task_09/main.c
View file @
c668cf51
...
...
@@ -25,7 +25,7 @@
/***** TYPEDEFS ***************************************************************/
/* 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 */
typedef
struct
cmd
{
// Command (string)
...
...
@@ -53,6 +53,7 @@ void stack_print (cmd_t *head);
* @param command Pointer to the desired command *
* @retval OK Pushing successful *
* @retval FULL Pushing failed -- stack is full *
* @retval FAILED Pushing failed -- allocation failed *
******************************************************************************/
retval_t
stack_push
(
cmd_t
**
head
,
char
*
command
)
{
/* Temporary pointer to the current stack element */
...
...
@@ -75,6 +76,11 @@ retval_t stack_push (cmd_t **head, char *command) {
/* Allocate memory for the new command */
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 */
strcpy
(
curr
->
command
,
command
);
/* Set the pointers accordingly */
...
...
@@ -146,6 +152,7 @@ int main (void) {
/*** Local Variables ***/
cmd_t
*
head
=
NULL
;
cmd_t
*
next
;
retval_t
ret
;
char
select
=
'x'
;
char
command
[
STRING_MAX
];
...
...
@@ -170,12 +177,17 @@ int main (void) {
/* Read in the user's input */
scanf
(
"%s"
,
command
);
/* (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
(
"
\n
PUSHED
\n\n
"
);
}
else
if
(
ret
==
FULL
)
{
/* Command cannot be pushed, stack is full */
printf
(
"
\n
FAILED -- Stack is already full!
\n\n
"
);
}
else
{
/* Command
has been successfully pushed
*/
printf
(
"
\n
PUSHED
\n\n
"
);
/* Command
cannot be pushed, allocation error
*/
printf
(
"
\n
FAILED -- Cannot allocate memory!
\n\n
"
);
}
/** Finished inserting **/
break
;
...
...
ch_6/task_10/main.c
View file @
c668cf51
...
...
@@ -25,7 +25,7 @@
/***** TYPEDEFS ***************************************************************/
/* 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 */
typedef
struct
element
{
// ID
...
...
@@ -62,6 +62,7 @@ void queue_print (element_t *head);
* @retval OK Pushing successful *
* @retval FULL Pushing failed -- stack is full *
* @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
)
{
/* Temporary pointer to the current queue element */
...
...
@@ -71,6 +72,13 @@ retval_t queue_insert (element_t **head, char *string, int priority) {
/* Allocate memory for the new command */
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 */
strcpy
(
new
->
string
,
string
);
/* Copy the priority */
...
...
@@ -253,6 +261,9 @@ int main (void) {
if
(
ret
==
FULL
)
{
/* Cannot be added, queue is full */
printf
(
"
\n
FAILED -- QUEUE is already full!
\n
"
);
}
else
if
(
ret
==
FAILED
)
{
/* Cannot be added, could not allocate memory */
printf
(
"
\n
FAILED -- Could not allocate memory!
\n
"
);
}
else
if
(
ret
==
OLDREMOVED
)
{
/* Added, but an old element was removed */
printf
(
"
\n
INSERTED -- But an old element was removed!
\n
"
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment