Hey, I didn't find an existing thread for this, but here's a nice macro I whipped up while doing some cleanup... one of the common uses for the ever popular temp buffer is code that looks like this:
sprintf( buf, "%s %s", obj->name, "rename" );
STRFREE( obj->name );
obj->name = STRALLOC( buf );
Now, a little while ago I extended the STRALLOC macro (and the underlying str_alloc and str_dup functions) to use varargs, since I hate temp buffers... so it occured to me that the only reason the temp buffer is needed here is because you're using the destination buffer in the creation of the source material. BUT, since the game driver isn't multi-threaded... why keep so many seperate temp buffers that all do the same thing? Hence, this macro was born:
* This code allows the replacement of an allocated pointer
* by a newly allocated pointer which might have used the original
* as a source.
#define STRREP(orig, point, ...)
strrep_tmp = (orig);
(orig) = STRALLOC( (point), # );
This nicely saves a pointer to the old value, allocs a new one (doing expansion via vsnprintf of the args), assigns the result to the original pointer, and then frees the original memory.
BTW: the use of varargs macros is a GNU extension to gcc, but it was formalized into the C99 standard.. so any reasonable modern compiler should do it.
Of course, neither of these ideas is necessary... and one can even argue the the resulting code after macro expansion is very slighly more expensive, but it looks much more compact and simple (thus being easier to maintain), and I think the reduction in the number of multi-kilobyte buffers may be worth the few extra CPU cycles.
Oh yeah, in case it isn't obvious... here's what the use of that macro looks like:
STRREP( obj->name, "%s %s", obj->name, "rename" );