Warning: mysqli::__construct(): (HY000/1203): User howardkn already has more than 'max_user_connections' active connections in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\includes\artfuncs.php on line 21
Failed to connect to MySQL: (1203) User howardkn already has more than 'max_user_connections' active connections
Warning: mysqli::query(): Couldn't fetch mysqli in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\index.php on line 66
Article <86h6boszrb.fsf@linuxsc.com>
Deutsch   English   Français   Italiano  
<86h6boszrb.fsf@linuxsc.com>

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

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups: comp.lang.c
Subject: Re: relearning C: why does an in-place change to a char* segfault?
Date: Tue, 13 Aug 2024 17:40:24 -0700
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <86h6boszrb.fsf@linuxsc.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>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Wed, 14 Aug 2024 02:40:27 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="088cfa383a3af87f6acebe452dc28057";
	logging-data="158184"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX193dKvqPSQAqwEPUmYbwikmDcYOHfiFyuk="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:WrXnqjEhIr+WQ0w+dREKPzN7Sb0=
	sha1:wiTDMz7rdHEqmUOZtGgvvSzMCTs=
Bytes: 3324

Vir Campestris <vir.campestris@invalid.invalid> writes:

> On 12/08/2024 22:11, Tim Rentsch wrote:
>
>> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>>
>> [...]
>>
>>> A string literal creates an array object with static storage
>>> duration.  [...]
>>
>> A small quibble.  Every string literal does sit in an array,
>> but it might not be a _new_ array, because different string
>> literals are allowed to overlap as long as the bytes in the
>> overlapping arrays have the right values.
>
> And this is exactly why string literals should always have been
> const.

The people who wrote the C standard reached a different
conclusion, and IMO the right one.

> A compiler is entitled to share memory between strings.  so
>
>     puts("lap");
>     puts("overlap");
>
> it's entitled to make them overlap.  Then add
>
>     char * p = "lap";
>     *p='X';
>
> and it can overwrite the shared string.  I think.  which would
> mean that writing "lap" again would have a different result.

A C implementation is also allowed to put every string literal
in its own separate array object, not shared even when two
or more string literals are identical, and make them writable
so they can be modified without problems.  I believe some C
compilers actually did this, perhaps under the control of a
compilation option.

> But that ship has sailed.  I'm not even sure const had been
> invented that far back!

C was already well established before 'const' was invented, and it
was a number of years after that before some C compilers started
allowing 'const' in source code.  The cost of not being backward
compatible would be high;  the cost adding const incrementally in
new code is low.  Generally speaking using string literals in open
code is a bad idea anyway, regardless whether there is any concern
that the string might be modified.  I think most people who want
string literals to be of type const char[] are only thinking about
one side of the equation.  It's always important to remember to
look at both sides of the cost/benefit forces, and not focus on
just the (imagined) benefits or (imagined) downsides.