Path: utzoo!attcan!uunet!lll-winken!lll-lcc!ames!husc6!rutgers!att!ihlpb!nevin1 From: nevin1@ihlpb.ATT.COM (Liber) Newsgroups: comp.lang.c Subject: Re: hardcoded constants Message-ID: <9244@ihlpb.ATT.COM> Date: 22 Dec 88 00:38:10 GMT References: <2636@m2-net.UUCP> <1104@goofy.megatest.UUCP> Reply-To: nevin1@ihlpb.UUCP (55528-Liber,N.J.) Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 24 In article <1104@goofy.megatest.UUCP> djones@megatest.UUCP (Dave Jones) writes: >From article <2636@m2-net.UUCP>, by remmers@m-net.UUCP (John H. Remmers): >> #define Catspace(s,t) (strlen(s) + strlen(t) + 1) >You are a candidate for SLMA (Silly Little Macros Anonymous). [:-)] >But if you don't like the naked "1", how about this? >foo = malloc( strlen(s) + strlen(t) + sizeof('\0')); >Tells it all. And keeps the reader on track, not grepping around >for macros. It also happens to be WRONG! (Actually, if you are only using it for malloc(), it will work; you just end up allocating more space than is actually needed.) Quoting from dpANS C 10/88 draft, section 3.1.3.4 (character constants), subsection on semantics (p 30): "An integer character constant has type int." This means, among other things, that sizeof('\0') == sizeof(int), and not sizeof(char). -- NEVIN ":-)" LIBER AT&T Bell Laboratories nevin1@ihlpb.ATT.COM (312) 979-4751