Deutsch   English   Français   Italiano  
<20240813200959.565@kylheku.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Kaz Kylheku <643-408-1753@kylheku.com>
Newsgroups: comp.lang.c
Subject: Re: relearning C: why does an in-place change to a char* segfault?
Date: Wed, 14 Aug 2024 03:16:26 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <20240813200959.565@kylheku.com>
References: <IoGcndcJ1Zm83zb7nZ2dnZfqnPWdnZ2d@brightview.co.uk>
 <v8fhhl$232oi$1@dont-email.me> <v8fn2u$243nb$1@dont-email.me>
 <87jzh0gdru.fsf@nosuchdomain.example.com> <865xs54fak.fsf@linuxsc.com>
 <v9fqtb$3t7ph$4@dont-email.me> <86h6boszrb.fsf@linuxsc.com>
 <87jzgjg9k3.fsf@nosuchdomain.example.com>
Injection-Date: Wed, 14 Aug 2024 05:16:27 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="06859f37ac14c6df67725d10cc8ff0a9";
	logging-data="322923"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/TtmloCfnD6MdHDvuZmEh5ELQ8Rei7iwk="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:xr2HZrkUmvvLen7m3XrF/qPiYTg=
Bytes: 2567

On 2024-08-14, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
> I can't speak for most people, but I want string literals to be const
> and I've thought about both sides of the equation.  (Existing code could
> be compiled with options to enable the old behavior and could be changed
> incrementally.)

C++ made string literals const sometime in the early 2000s.

That makes it much easier to be in favor of the change; it not
only helps prevent bugs, but improves C and C++ compatibility.

When programmers write string manipulating functions, they tend
to test them with string literal arguments. When string literals
are const, that encourages the programmers to make arguments
const whenever they can be which tends to improve the functions.

I work in C codebases that are also compiled as C++, so const
string literals are second nature. It's old hat by now.

Also, <string.h> could have type generic functions where it
makes sense to support both const char * and char *.

E.g. strchr should could return const char * if the
parameter is const char *, and char * when the parameter is char *.
The one function we have now strips the qualifier, which is bad;
when you find a character in a const string, you get a non-const
pointer to it.

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca