hardcoded constants
Liber
nevin1 at ihlpb.ATT.COM
Thu Dec 22 10:38:10 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):
>> #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 at ihlpb.ATT.COM (312) 979-4751
More information about the Comp.lang.c
mailing list