Deutsch English Français Italiano |
<20240801174256.890@kylheku.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: Kaz Kylheku <643-408-1753@kylheku.com> Newsgroups: comp.lang.c Subject: Re: relearning C: why does an in-place change to a char* segfault? Date: Fri, 2 Aug 2024 01:06:08 -0000 (UTC) Organization: A noiseless patient Spider Lines: 49 Message-ID: <20240801174256.890@kylheku.com> 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> Injection-Date: Fri, 02 Aug 2024 03:06:08 +0200 (CEST) Injection-Info: dont-email.me; posting-host="06869e1ea80aff81fac5e88c79ec8ec1"; logging-data="2588217"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18UXYCA1Ox3cXMBpal31k3VQFFs+okQ4es=" User-Agent: slrn/pre1.0.4-9 (Linux) Cancel-Lock: sha1:b4Xyg9okLGNhn0e+RJH9yD6iBpA= Bytes: 2933 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. There is no portable way to request that. C++ implementations have late initialization for block scope statics. A program which somehow gains access to the initialization data for those, and modifies it, would be squarely in undefined behavior territory. In mainstream C implementations there typically isn't a separate storage for the initialization data for statics. They are set up before the program runs. -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @Kazinator@mstdn.ca