Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!mit.eternal-september.org!.POSTED!not-for-mail From: Richard Newsgroups: comp.lang.c Subject: Re: Constant strings Date: Sun, 20 Apr 2014 11:08:47 +0100 Organization: http://www.ieee.org/ Lines: 55 Message-ID: <87ppkcb9e8.fsf@gmail.com> References: <20140418155718.971@kylheku.com> <9fd76d44-6d6b-466e-8515-0b3a806854cd@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mit.eternal-september.org; posting-host="bdee7d74bb5599725cdba9fe2a37525e"; logging-data="22098"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/MaylHcP+RWwTl2ED8dfqMV5rePd+OheM=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:+3ZjfAfpEV5p0XfMWLeuA4Gk9R4= sha1:gtVqLHxrxzzZNtArb6r9ac4FQGI= Xref: csiph.com comp.lang.c:43169 Keith Thompson writes: > "BartC" writes: >> "Keith Thompson" wrote in message >> news:lneh0t3wcs.fsf@nuthaus.mib.org... >>> Malcolm McLean writes: >> >>>> This is the sort of thing that makes languages which attempt to be a >>>> safer C difficult to use. The programmer is constantly programming >>>> around the restrictions. >>> >>> So what's your solution? Would you drop "const" from the language? >> >> Am I allowed to say Yes? > > Certainly -- and I'm allowed to say that I completely disagree. > > (Incidentally, the question was addressed to Malcolm, which in no way > implies that your input is unwelcome.) > >> I don't think it would be missed. It's easy enough >> though to just not use it, or to remove const keywords with an editor (then >> maybe the code will be a lot clearer!). >> >>> Suppose you have a function that modifies a string, and you accidentally >>> pass it a string literal; do you not *want* the compiler to warn you? >> >> There are plenty of other things to worry about. If you are calling a >> function which does in-place modifications of an argument, then you need to >> know about it, as you might not want that to happen even if your string is >> writeable; you just don't want it written to by that function. > > Exactly -- and you can express that by defining that function's > parameter as "const char*". > > char s[] = "hello"; /* s is writable */ > strlen(s); /* strlen() promises not to modify the string */ > >> Modification of a string literal at least has a chance of being detected by >> the hardware. > > Yes, but not at compile time. I like to detect errors as early as > possible. > >> Using 'const's might just be giving a false sense of security. > > I might be hit by a meteorite; why bother to wear a seatbelt? Wowa. Something I agree with. Use it properly. And who cares what Richie says either. Kernighan's quote about debuggers is as worthless in this day and age. Times change. Tools change. hw changes. -- "Avoid hyperbole at all costs, its the most destructive argument on the planet" - Mark McIntyre in comp.lang.c