Path: utzoo!attcan!lsuc!ncrcan!ziebmef!cks From: cks@ziebmef.uucp (Chris Siebenmann) Newsgroups: comp.lang.c Subject: Re: hardcoded constants Message-ID: <1988Dec26.021757.15813@ziebmef.uucp> Date: 26 Dec 88 07:17:54 GMT References: <1988Dec8.173158.11839@utzoo.uucp> <846@starfish.Convergent.COM> <9134@smoke.BRL.MIL> <1988Dec13.172306.16195@utzoo.uucp> <5146 <2636@m2-net.UUCP> Reply-To: cks@ziebmef.UUCP (Chris Siebenmann) Organization: Ziebmef Public Access Unix, Toronto, Ontario Lines: 26 In article <2636@m2-net.UUCP> remmers@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@ziebmef.UUCP or .....!utgpu!{,ontmoh!,ncrcan!brambo!}cks