Deutsch   English   Français   Italiano  
<v8i9o8$2oof8$1@dont-email.me>

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

Path: ...!feeds.phibee-telecom.net!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: relearning C: why does an in-place change to a char* segfault?
Date: Fri, 2 Aug 2024 10:43:36 +0100
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <v8i9o8$2oof8$1@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> <v8gte2$2ceis$2@dont-email.me>
 <20240801174256.890@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 02 Aug 2024 11:43:37 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="685cfd46d568320119a7f0cfbd8429d5";
	logging-data="2908648"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/orC5ou2feaKGCkGGOkg0N"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:/X6shnPBuK+8F/pFQF1Vq+wsrsg=
In-Reply-To: <20240801174256.890@kylheku.com>
Content-Language: en-GB
Bytes: 3731

On 02/08/2024 02:06, Kaz Kylheku wrote:
> On 2024-08-01, Bart <bc@freeuk.com> wrote:
>>>> It segfaults when the string is stored in a read-only part of the binary.
>>>
>>> A string literal creates an array object with static storage duration.
>>> Any attempt to modify that array object has undefined behavior.
>>
>> What's the difference between such an object, and an array like one of
>> these:
> 
> Programming languages can have objects that have the same lifetime, yet some
> of which are mutable and some of which are immutable.
> 
> If the compiler believes that the immutable objects are in fact
> not mutated, it's a bad idea to modify them behind the compiler's
> back.
> 
> There doesn't have to be any actual difference in the implementation of
> these objects, like in what area they are stored, other than the rules
> regarding their correct use, namely prohibiting modification.
> 
> The Racket language has both mutable and immutable cons cells.
> The difference is that the immutable cons cells simply lack the
> operations needed to mutate them. I'm not an expert on the Racket
> internals but I don't see a reason why they couldn't be stored in the
> same heap.
> 
>>    static char A[100];
>>    static char B[100]={1};
>>
>> Do these not also have static storage duration? Yet presumably these can
>> be legally modified.
> 
> That 1 which initializes B[0] cannot be modified.
> 

Why not? I haven't requested that those are 'const'. Further, gcc has no 
problem running this program:

     static char A[100];
     static char B[100]={1};

     printf("%d %d %d\n", A[0], B[0], 1);
     A[0]=55;
     B[0]=89;
     printf("%d %d %d\n", A[0], B[0], 1);

But it does use readonly memory for string literals.

(The point of A and B was to represent .bss and .data segments 
respectively. A's data is not part of the EXE image; B's is.

While the point of 'static' was to avoid having to specify whether A and 
B were at module scope or within a function.)

 > That 1 which initializes B[0] cannot be modified.

Or do you literally mean the value of that '1'? Then it doesn' make 
sense; here that is a copy of the literal stored in one cell of 'B'. The 
value of the cell can change, then that particular copy of '1' is lost.

Here:

     static char B[100] = {1, 1, 1, 1, 1, 1};

changing B[0] will not affect the 1s in B[1..5], and in my example 
above, that standalone '1' is not affected.