home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / std / c / 3025 < prev    next >
Encoding:
Text File  |  1992-11-17  |  1.9 KB  |  44 lines

  1. Newsgroups: comp.std.c
  2. Path: 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
  3. From: diamond@jit345.bad.jit.dec.com (Norman Diamond)
  4. Subject: Re: Casting const; Is this standard conforming?
  5. Message-ID: <1992Nov17.065821.16979@nntpd.lkg.dec.com>
  6. Sender: usenet@nntpd.lkg.dec.com (USENET News System)
  7. Reply-To: diamond@jit.dec.com (Norman Diamond)
  8. Organization: Digital Equipment Corporation Japan , Tokyo
  9. References: <1992Nov16.200843.5565@Princeton.EDU>
  10. Date: Tue, 17 Nov 1992 06:58:21 GMT
  11. Lines: 31
  12.  
  13. In article <1992Nov16.200843.5565@Princeton.EDU> rhl@grendel.Princeton.EDU (Robert Lupton the Good) writes:
  14. >#include <stdio.h>
  15. >   const int i = 0;
  16. >   (int)i = 1;                /* Fails */
  17. >   *(int *)&i = 1;            /* OK */
  18. >Thinking about ROMs my guess is that gcc and the IBM xlc compilers are
  19. >correct to reject the former assignment,
  20.  
  21. Thinking about the standard, where it says that the result of a const is
  22. not an lvalue, you should know that every conforming implementation is
  23. correct to reject the former assignment.
  24.  
  25. >and that their acceptance of the latter is permitted,
  26.  
  27. Yes.  The latter assignment is allowed to have any effect at all, without
  28. even needing to be documented by an implementation.  Acceptance is legal.
  29.  
  30. >What do you think about the portability of the cast?
  31.  
  32. The cast is portable.  The attempted modification of a const object (i)
  33. through use of a non-const lvalue (*(int *)&i) is allowed to succeed,
  34. fail, dump core, or print some advice that you should buy a standard.
  35.  
  36. >(I want to set the variable once in a private place just after mallocing it,
  37.  
  38. malloc a non-const variable, set it once, and then copy the address to a
  39. pointer-to-const-int.  Then pass around the const version.
  40. --
  41. Norman Diamond       diamond@jit081.enet.dec.com
  42. If this were the company's opinion, I wouldn't be allowed to post it.
  43. "It's been a lovely recession."
  44.