🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2958

Re: Casting const; Is this standard conforming?

#2958
From: Norman Diamond
Date: Tue, 17 Nov 1992 06:58
31 lines
2037 bytes
In article <1992Nov16.200843.5565@Princeton.EDU> rhl@grendel.Princeton.EDU (Robert Lupton the Good) writes:
>#include <stdio.h>
>   const int i = 0;
>   (int)i = 1;				/* Fails */
>   *(int *)&i = 1;			/* OK */
>Thinking about ROMs my guess is that gcc and the IBM xlc compilers are
>correct to reject the former assignment,

Thinking about the standard, where it says that the result of a const is
not an lvalue, you should know that every conforming implementation is
correct to reject the former assignment.

>and that their acceptance of the latter is permitted,

Yes.  The latter assignment is allowed to have any effect at all, without
even needing to be documented by an implementation.  Acceptance is legal.

>What do you think about the portability of the cast?

The cast is portable.  The attempted modification of a const object (i)
through use of a non-const lvalue (*(int *)&i) is allowed to succeed,
fail, dump core, or print some advice that you should buy a standard.

>(I want to set the variable once in a private place just after mallocing it,

malloc a non-const variable, set it once, and then copy the address to a
pointer-to-const-int.  Then pass around the const version.
--
Norman Diamond       diamond@jit081.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
"It's been a lovely recession."
Technical Details
Message-ID: <1992Nov17.065821.16979@nntpd.lkg.dec.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!zaphod.mps.ohio-state.edu!darwin.sura.net!haven.umd.edu!decuac!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond
References: <1992Nov16.200843.5565@Princeton.EDU>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!zaphod.mps.ohio-state.edu!darwin.sura.net!haven.umd.edu!decuac!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond
From: diamond@jit345.bad.jit.dec.com (Norman Diamond)
Subject: Re: Casting const; Is this standard conforming?
Message-ID: <1992Nov17.065821.16979@nntpd.lkg.dec.com>
Sender: usenet@nntpd.lkg.dec.com (USENET News System)
Reply-To: diamond@jit.dec.com (Norman Diamond)
Organization: Digital Equipment Corporation Japan , Tokyo
References: <1992Nov16.200843.5565@Princeton.EDU>
Date: Tue, 17 Nov 1992 06:58:21 GMT
Lines: 31