Re: how to replace a substring in a string using C?



Netocrat wrote:
<snip>
char *replace(const char *s, const char *old, const char *new)
{
<snip>
    ret = malloc(i + count * (newlen - oldlen));
    if (ret == NULL)
        exit(EXIT_FAILURE);


Whoa, what's this? Just return NULL. It's massively rude to terminate the program in a function like this. It's *likely* the caller can't respond to out-of-memory either (and won't check for it if they're sloppy), but making the decision here is silly.


S.
.



Relevant Pages

  • Re: Display of Caller ID
    ... > info regarding Caller ID, it just said that if the number is in the ... Thanks for all replies, replying to one is a lot easier then replying to ... The number/name in the phonebook is correct and in the correct format, ...
    (uk.telecom)
  • [PATCH 21/23] tracehook: wait_task_inactive
    ... make sure the task is off any CPU ... UNPROTECT_CTX(ctx, flags); ... if (!ret &&!kill) ... The caller must ensure that the task *will* unschedule sometime soon, ...
    (Linux-Kernel)
  • Re: [PATCH 2/5] memcg: migration account fix
    ... return ret; ... ptr would be kept unset when!PageCgroupUsed. ... (unmap_and_move, caller of prepare_migration, doesn't initilize it.) ...
    (Linux-Kernel)
  • Re: Big Brother!!!
    ... got to the scene rapidly even if the caller can't speak or the line goes ... caller actually is - a mobile phone can be tracked using its own signal, ... handled weak signals, I'd guess. ...
    (uk.people.support.depression)
  • Re: const argument
    ... Stefan Ram wrote: ... snip ... ... > Regarding the other const: a parameter in a sense is a message ... > from the caller to the called function. ...
    (comp.lang.c)