Why leave the error handling to the caller?



On to top of page 163 in the book "The C Programming Langauge" by K &
R, they have the following:

char *strdup(char *s)
{
char *p;
p=(char *)malloc(strlen(s)+1);
if( p != NULL)
strcpy(p,s):
return p;
}

They then go on to say

"strdup passes that value on, leaving error-handling to its caller"

Why leave the error-handling to it's caller? Couldn't a person go like

char *strdup(char *s)
{
char *p;
if (p=(char *)malloc(strlen(s)+1) == NULL)
exit(1);
else
strcpy(p,s):
return p;
}


Chad

.



Relevant Pages

  • Re: string compare
    ... short int length; ... char data; ... You can have the caller of the library routines specify which type ... Well what if someone wants to compare a short string with a long ...
    (comp.lang.c)
  • Re: String array
    ... be stored in (char ***fragments) which is passed as an argument. ... in GDB to see the caller, the second element of the array is either ... How can I dynamically allocate an array of ... or you can pass a pointer to the variable you want it stored ...
    (comp.lang.c)
  • Re: Why leave the error handling to the caller?
    ... char *strdup ... Why leave the error-handling to it's caller? ... There quite a strong case for a safemalloclibrary function that does terminate with an error message on fail. ... More modern languages have exception handling, which is the best solution, in the right hands. ...
    (comp.lang.c)
  • Re: String array
    ... stored in (char ***fragments) which is passed as an argument. ... GDB to see the caller, the second element of the array is either null ... How can I dynamically allocate an array of strings ...
    (comp.lang.c)
  • Re: String array
    ... stored in (char ***fragments) which is passed as an argument. ... GDB to see the caller, the second element of the array is either null ... How can I dynamically allocate an array of strings ...
    (comp.lang.c)