hardcoded constants

Chris Siebenmann cks at ziebmef.uucp
Mon Dec 26 17:17:54 AEST 1988


In article <2636 at m2-net.UUCP> remmers at m-net.UUCP (John H. Remmers) writes:
...
>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)
>In terms of this, the malloc() call can be written:
>	foo = malloc(Catspace(s,t) + sizeof('/'));

 For true safety, I'd write this as
 
 	foo = malloc(Catspace(s,t)*sizeof(char) + sizeof('/'));

(or redefine Catspace() to do this itself, depending on whether it
returns a character or byte count), just in case someone produces a
compiler where characters aren't single bytes. 

[Corrections gratefully accepted; I don't think ANSI mandated
sizeof(char) being one, although a lot of code will probably break if
it isn't.]
[Route uucp mail manually until the January uucp maps come out; a
machine on the favorite route to the Ziebmef just went away.]
-- 
	"...in all the history of Earth, there's never been a heaven, never
	    been a house of gods that was not built on human bones."
Chris Siebenmann		uunet!{utgpu!moore,attcan!telly}!ziebmef!cks
cks at ziebmef.UUCP	     or	.....!utgpu!{,ontmoh!,ncrcan!brambo!}cks



More information about the Comp.lang.c mailing list