Deutsch English Français Italiano |
<v8j8pg$2uq6r$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!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 19:33:20 +0100 Organization: A noiseless patient Spider Lines: 58 Message-ID: <v8j8pg$2uq6r$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> <v8i9o8$2oof8$1@dont-email.me> <v8j808$2us0r$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 02 Aug 2024 20:33:21 +0200 (CEST) Injection-Info: dont-email.me; posting-host="685cfd46d568320119a7f0cfbd8429d5"; logging-data="3107035"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18v+WF+f59GP6WUymfyuzA5" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:+WUNh+CuK7oxdQmNGF9YEhJn/Mc= In-Reply-To: <v8j808$2us0r$1@dont-email.me> Content-Language: en-GB Bytes: 3576 On 02/08/2024 19:19, James Kuyper wrote: > On 8/2/24 5:43 AM, Bart wrote: >> 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: >>>> 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'. ... > > You don't get a choice in the matter. The C language doesn't permit > numeric literals of any kind to be modified by your code. My post wasn't about numerical literals. I assumed it was about that '1' value which is stored B's first cell. However, just in case KK was talking about that unlikely possibly, I covered that as well. ey can't be, > and don't need to be, declared 'const'. I've heard that in some other > languages, if you call foo(3), and foo() changes the value of it's > argument to 2, then subsequent calls to bar(3) will pass a value of 2 to > bar(). That sounds like such a ridiculous mis-feature that I hesitate to > identify which languages I had heard accused of having that feature, but > it is important to note that C is not one of them. > > Just as 1 is an integer literal whose value cannot be modified, It can't modified, in a value that would also affect other instances of '1' within that module or produce, because it is very unlikely to be shared. I don't know of any implementations of this kind of language which do that. (The nearest might FORTRAN IV when '1' was passed by reference to a subroutine, and the subroutine then assigns to that parameter.) Where it would be more plausible is here: const char* B[] = {"A", "A", "A"}; where if you can somehow change that first "A", then the other two could also change if the compiler decides to share those 3 identical strings.