| Deutsch English Français Italiano |
|
<c1578de631eaf76975429819e7939acdf1c46897@i2pn2.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!weretis.net!feeder9.news.weretis.net!i2pn.org!i2pn2.org!.POSTED!not-for-mail From: Richard Damon <richard@damon-family.org> Newsgroups: comp.lang.c Subject: Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault? Date: Fri, 2 Aug 2024 15:11:20 -0400 Organization: i2pn2 (i2pn.org) Message-ID: <c1578de631eaf76975429819e7939acdf1c46897@i2pn2.org> References: <IoGcndcJ1Zm83zb7nZ2dnZfqnPWdnZ2d@brightview.co.uk> <20240801174026.00002cda@yahoo.com> <v8gi7i$29iu1$1@dont-email.me> <slrnvaorkl.34j6.candycanearter07@candydeb.host.invalid> <87zfpvfdk4.fsf@nosuchdomain.example.com> <v8ii17$2q5p1$1@dont-email.me> <87v80ig4vt.fsf@nosuchdomain.example.com> <82cc9501de86336cdef9fe610bce8e75238fb679@i2pn2.org> <v8ja83$2us0r$2@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 2 Aug 2024 19:11:20 -0000 (UTC) Injection-Info: i2pn2.org; logging-data="1215790"; mail-complaints-to="usenet@i2pn2.org"; posting-account="diqKR1lalukngNWEqoq9/uFtbkm5U+w3w6FQ0yesrXg"; User-Agent: Mozilla Thunderbird In-Reply-To: <v8ja83$2us0r$2@dont-email.me> X-Spam-Checker-Version: SpamAssassin 4.0.0 Content-Language: en-US Bytes: 2702 Lines: 34 On 8/2/24 2:58 PM, James Kuyper wrote: > On 8/2/24 14:42, Richard Damon wrote: >> On 8/2/24 2:24 PM, Keith Thompson wrote: >>> Richard Harnden <richard.nospam@gmail.invalid> writes: >>> [...] >>>> Is there any reason not to always write ... >>>> >>>> static const char *s = "hello, world"; >>>> >>>> ... ? > ... >>> There's no good reason not to use "const". (If string literal objects >>> were const, you'd have to use "const" here.) > ... >> The one good reason to not make it const is that if you are passing it >> to functions that take (non-const) char* parameters that don't >> actually change that parameters contents. > > Actually, that's not a good reason. If you can't modify the function's > interface, you should use a (char*) cast, which will serve to remind > future programmers that this is a dangerous function call. You shouldn't > make the pointer's own type "char *". > > Depends on the library and how many times it is used. It may be a perfectly safe call, as the function is defined not to change its parameter, but being external code the signature might not be fixable. Adding the cast at each call, may cause a "crying wolf" response that trains people to just add the cast where it seems to be needed (even if not warrented). You likely DO want a note at the statement explaining the situation.