Re: [ILUG] C string concat question ??

From: Paul J Collins (sneakums at domain zork.net)
Date: Fri 04 May 2001 - 10:48:01 IST


>>>>> "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.

-- 
 /////////////////  |                  | The spark of a pin
<sneakums at domain zork.net> |   left blank.    | dropping, falling feather-like.
 \\\\\\\\\\\\\\\\\  |                  | There is too much noise.


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