Re: [ILUG] C string concat question ??

From: Smelly Pooh (plop at domain redbrick.dcu.ie)
Date: Fri 04 May 2001 - 10:53:46 IST


In reply to Paul J Collins's flatulent wordings,
> >>>>> "SP" == Smelly Pooh <plop at domain redbrick.dcu.ie> writes:
>
> SP> In reply to Dave Neary's flatulent wordings,
> >> Dave Neary wrote:
> >> > strcat((msg=strdup(strvar1),
> >> > (msg=realloc(msg,strlen(msg)+strlen(strvar2)+1))==NULL?
> >> > bad_stuff():msg),strvar2);
> >>
> >> works :) I was only confusing myself with the whole "order of
> >> evaluation" red herring, since the first arg doesn't go near the second
> >> arg. But anyone who ever used this should be shot :)
>
> SP> If order of evaluation was right to left however then you will
> SP> be realloccing an unallocated msg, strlen() msg which could
> SP> lead to a seg fault if msg doesn't point somewhere valid and
> SP> generaly make things quite sucky
>
> The comma operator is totally different to the comma that separates
> arguments to a function. Expressions are sequenced by the comma
> operator and are executed from left-to-right, in order. The strdup
> will thus always evaluate before the realloc. Nevertheless, it is
> bad, nasty code that does not buy you anything except, perhaps, job
> security.

Apologies, I didn't see the left bracket indicating that the comma was the
comma operator and not an argument separator, I guess we can all agree it's
pretty ugly code :)



This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:10:11 GMT