Deutsch   English   Français   Italiano  
<v8o13o$30mh$1@raubtier-asyl.eternal-september.org>

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

Path: ...!weretis.net!feeder9.news.weretis.net!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita Montero <Bonita.Montero@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: relearning C: why does an in-place change to a char* segfault?
Date: Sun, 4 Aug 2024 15:52:59 +0200
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <v8o13o$30mh$1@raubtier-asyl.eternal-september.org>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 04 Aug 2024 15:52:56 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="cca460a5e73b1f123396303db8231286";
	logging-data="99025"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19hLiUD9Qb+/3BnVPGoWJO8IMqDipxlgjU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:9wtuSO0pi9kH3f9KQEI+TBtadKc=
In-Reply-To: <v8gte2$2ceis$2@dont-email.me>
Content-Language: de-DE
Bytes: 3165

Am 01.08.2024 um 23:07 schrieb Bart:
> On 01/08/2024 21:59, Keith Thompson wrote:
>> Bart <bc@freeuk.com> writes:
>>> On 01/08/2024 09:38, Richard Harnden wrote:
>>>> On 01/08/2024 09:06, Mark Summerfield wrote:
>>>>> This program segfaults at the commented line:
>>>>>
>>>>> #include <ctype.h>
>>>>> #include <stdio.h>
>>>>>
>>>>> void uppercase_ascii(char *s) {
>>>>>       while (*s) {
>>>>>           *s = toupper(*s); // SEGFAULT
>>>>>           s++;
>>>>>       }
>>>>> }
>>>>>
>>>>> int main() {
>>>>>       char* text = "this is a test";
>>>>>       printf("before [%s]\n", text);
>>>>>       uppercase_ascii(text);
>>>>>       printf("after  [%s]\n", text);
>>>>> }
>>>> text is pointing to "this is a test" - and that is stored in the
>>>> program binary and that's why can't modify it.
>>>
>>> That's not the reason for the segfault in this case.
>>
>> I'm fairly sure it is.
>>
>>>                                                       With some
>>> compilers, you *can* modify it, but that will permanently modify that
>>> string constant. (If the code is repeated, the text is already in
>>> capitals the second time around.)
>>>
>>> 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:
> 
>   static char A[100];
>   static char B[100]={1};

This char arrays are modifyable because they're not const.

> Do these not also have static storage duration? Yet presumably these can 
> be legally modified.
>