Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: So You Think You Can Const? Date: Wed, 08 Jan 2025 13:01:15 -0800 Organization: None to speak of Lines: 35 Message-ID: <87tta9qauc.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Wed, 08 Jan 2025 22:01:20 +0100 (CET) Injection-Info: dont-email.me; posting-host="78dde0aa8fcfd62296c6657e38e15b03"; logging-data="3093577"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+1sZ09pxJfaHGzypKyMn0k" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:1rMMzdPrEf+EhqWqYHfEW9eK76g= sha1:MydhcUpjlZuipJ8Fx5j+kiI+T+g= Xref: csiph.com comp.lang.c:389902 Andrey Tarasevich writes: > On 01/07/25 11:32 AM, Julio Di Egidio wrote: >> To the question, I was reading this, but I am not >> sure what the quoted passage means: >> Matt Stancliff, "So You Think You Can Const?", >> >> << Your compiler, at its discretion, may also choose >>    to place any const declarations in read-only storage, >>    so if you attempt to hack around the const blocks, >>    you could get undefined behavior. >> > > Strictly speaking, the passage is misleading. It dues not matter > whether the compiler decided to place const data into read-only > storage. If you "hack around" data constness (i.e. if you attempt to > modify const data), you _always_ get undefined behavior, regardless of > where the data is actually stored. And one possible result of undefined behavior (in some sense perhaps the worst) is that the code behaves just as you expected it to. The author of the article likely thought of "undefined behavior" as "the program crashes" or "something goes terribly wrong". In fact undefined behavior is simply behavior that is not defined; the C standard says nothing about what happens. And if the manifestation of that undefined behavior is that the code quietly does what you thought it would do, it could mean that you have a latent bug that's difficult to track down, and that will come back and bite you later. [...] -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */