Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: So You Think You Can Const? Date: Sun, 12 Jan 2025 18:26:33 -0800 Organization: A noiseless patient Spider Lines: 14 Message-ID: <86jzaz78km.fsf@linuxsc.com> References: <20250110103252.718@kylheku.com> <20250111182556.466@kylheku.com> <87ldvgk7b3.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Mon, 13 Jan 2025 03:26:36 +0100 (CET) Injection-Info: dont-email.me; posting-host="9eea8443f476a77794ac96904ee9ae64"; logging-data="1592529"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18AM+ZZj0e/O6zk1RVy+I8Z7MHEsFmyjOM=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:rwGBDUau8HuGmvrmxdbJ7nzl3oM= sha1:k5FWbjqvggaZaWHKwMTWXrgNm/M= Xref: csiph.com comp.lang.c:390000 Keith Thompson writes: > All the standard really says about "const" is that directly modifying > a const-qualified object is a constraint violation, and indirectly > modifying it has undefined behavior. [...] This description isn't exactly right. The constraint violation is for trying to modify an object through an lvalue that is not modifiable, whether or not the object being referenced is const qualified. There is undefined behavior for trying to modify a const object, but only for objects _defined_ with a const-qualified type. So the rule doesn't apply to any object that resides in allocated memory, because those objects are not named in any declaration, and thus not defined with a const-qualified type.