hardcoded constants

John H. Remmers remmers at m-net.UUCP
Sat Dec 24 03:04:37 AEST 1988


In article <1104 at goofy.megatest.UUCP> djones at megatest.UUCP (Dave Jones) writes:
>From article <2636 at m2-net.UUCP>, by remmers at m-net.UUCP (John H. Remmers):
>...
>> The space requirement for a string concatenation is a frequently-needed
>> value; it's worth having a macro to represent it:
>> 
>> 	#define  Catspace(s,t)  (strlen(s) + strlen(t) + 1)
>> 
>
>You are a candidate for SLMA (Silly Little Macros Anonymous).
>
The merits are debatable, I suppose, but if an application requires a 
lot of dynamic allocation of space for strings, hiding the 1 for the
null byte in a macro definition is a way to defend against forgetting
it (a common error, in my experience).  The wisdom of this approach
ultimately is determined by frequency of use.  Agreed, inventing
macros for isolated situations is not good practice.  What I was trying
to say, perhaps not too clearly, is that a macro for string concatena-
tion space might find sufficient use to be worth defining, and that
*if* that is the case, this is a natural place to use it.
>
>But if you don't like the naked "1", how about this?
>
>foo =  malloc( strlen(s) + strlen(t) + sizeof('\0'));
>
As a couple of people pointed out to me in mail, '\0' and '/' are
ints, so sizeof('\0') = sizeof(int), usually 2 or 4, and more space
is allocated than needed.

It was not the naked "1" I was questioning so much as the naked "2".



More information about the Comp.lang.c mailing list