Sujet : Re: relearning C: why does an in-place change to a char* segfault?
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 14. Aug 2024, 04:16:26
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240813200959.565@kylheku.com>
References : 1 2 3 4 5 6 7 8
User-Agent : slrn/pre1.0.4-9 (Linux)
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/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca