In my code almost every function has one or more malloc calls, and each time I have to do something like:
char *ptr = (char *)malloc(sizeof(char) * some_int);
if (ptr == NULL) {
fprintf(stderr, "failed to allocate memory.\n");
return -1;
}
that's four extra lines of code and if I add them everytime after I use a malloc, the length of my code will increase a lot.. so is there an elegant way to deal with this?
Thank you so much!!
If you error condition is always that simple (print error message and return) you can rewrite to save lines.