tobold.org

correct • elegant • free

△ Comp.lang.c △

◅ Q: Nested includes good or bad?

A few Questions ▻

A useful strcat function

In article <398830e4_2@news2.prserv.net>, Ioannis Vranos <not@available> wrote:
>    destination=(char *)calloc((size+1), sizeof(char));

You never test the value of destination after this assignment and before
using it.  What happens if calloc() fails?  (Answer: your function
crashes and burns.)

A few other comments on this line.  Why use calloc() when malloc()
would do just as well?  In particular, `sizeof(char)' is always 1, so
you don't need the (calloc() implicit) multiplication.  Finally, it's
considered bad style, in C, to cast the return value of the malloc()
family.  Wrapping all these up, this line should read something like
this...

    destination = malloc(size + 1);
    if (!destination) return 0;
    ...

Tim.
--
Tim Goodwin   | "Not ideal, I grant you, but life's
Leicester, UK | like that." -- Ian Batten

Original headers:

From: tjg@star.le.ac.uk (Tim Goodwin)
Newsgroups: comp.lang.c
Subject: Re: A useful strcat function
Date: 2 Aug 2000 16:40:36 +0100
Message-ID: <8m9fea$15i$1@ltpcg.star.le.ac.uk>
References: <398830e4_2@news2.prserv.net>

△ Comp.lang.c △

◅ Q: Nested includes good or bad?

A few Questions ▻