Deutsch   English   Français   Italiano  
<v9fqtb$3t7ph$4@dont-email.me>

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: Vir Campestris <vir.campestris@invalid.invalid>
Newsgroups: comp.lang.c
Subject: Re: relearning C: why does an in-place change to a char* segfault?
Date: Tue, 13 Aug 2024 15:34:19 +0100
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <v9fqtb$3t7ph$4@dont-email.me>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 13 Aug 2024 16:34:20 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e0038d546466fb789c935a51995a223b";
	logging-data="4103985"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+GW/ECAtXKkAoWQn9VixDOARnMoQh2nWg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:/xFj6mhYvvEOiXA/gkqvumtmc20=
In-Reply-To: <865xs54fak.fsf@linuxsc.com>
Content-Language: en-GB
Bytes: 2099

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.

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.

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

Andy