The code you already tested for the error, although I usually write the assignment and check as two separate lines:
new_list = malloc(sizeof *new_list); if (!new_list) ;
(pay attention to two small changes - you should not specify the return value, and we take the size of the variable, not its type, to reduce the likelihood of a mismatch).
If malloc() does not work, it returns a null pointer, which is the only pointer value that is false.
The error handling you have is simply return -1; - how do you deal with what is really up to you in the calling function.
Toby speech
source share