home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / bsd / 8517 < prev    next >
Encoding:
Text File  |  1992-11-07  |  4.0 KB  |  101 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!sun-barr!ames!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  2. From: torek@horse.ee.lbl.gov (Chris Torek)
  3. Newsgroups: comp.unix.bsd
  4. Subject: Re: mktemp - Bus Error.
  5. Date: 6 Nov 1992 12:29:24 GMT
  6. Organization: Lawrence Berkeley Laboratory, Berkeley
  7. Lines: 88
  8. Message-ID: <27268@dog.ee.lbl.gov>
  9. References: <KHERA.92Nov3111245@thneed.cs.duke.edu> <1992Nov3.174359.19262@Princeton.EDU> <9231017.24452@mulga.cs.mu.OZ.AU> <1992Nov5.180007.28471@fcom.cc.utah.edu>
  10. Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
  11. NNTP-Posting-Host: 128.3.112.15
  12.  
  13. In article <1992Nov5.180007.28471@fcom.cc.utah.edu> terry@cs.weber.edu
  14. (A Wizard of Earth C) writes:
  15. >Several compilers treat:
  16. >    char tmpname[] = "/tmp/foo.XXXXXX";
  17. >as if it were:
  18. >    char *tmpname = "/tmp/foo.XXXXXX";
  19.  
  20. Any compiler that does so is horribly broken.
  21.  
  22. >...the second is, of course, legal.
  23.  
  24. The first is also legal, provided that:
  25.  
  26.  a) the compiler claims to be ANSI X3.159-1989 (`ANSI C') conformant; or:
  27.  b) the array "tmpname" has static duration.
  28.  
  29. (This is an inclusive `or'.)
  30.  
  31. >    char *foo = "Hello World" + 6;
  32. >
  33. >    printf( "%s\n", foo);        /* prints "World\n"*/
  34. >    printf( "%s\n", foo - 6);    /* depends on code generation*/
  35.  
  36. This is entirely wrong.  The type of a string literal is now and has
  37. always been `array N of char'.  (There have been a few compilers that
  38. got this wrong, but they were/are broken and essentially irrelevant.
  39. No BSD compiler has been broken in this way.)  The characters
  40. themselves may be kept in read-only storage---this is the default under
  41. GCC, and is permitted by the ANSI C standard---or in read/write
  42. storage.  The old PCC-based BSD compilers did the latter, and GCC will
  43. also do so if you use `-fwritable-strings'.  Writable string literals
  44. are permitted by the ANSI standard---it does not pin implementations
  45. down either way---but since they *might* be read-only, you should
  46. treat them as read-only.
  47.  
  48. Note that the "/tmp/foo.XXXXXX" in
  49.  
  50.     char tmpname[] = "/tmp/foo.XXXXXX";
  51.  
  52. is *NOT* a string literal, despite the syntactic resemblance.  (It is
  53. an initializer.)
  54.  
  55. >... If the compiler generates code the K&R/Portable C compile way, the
  56. >second printf() will print "Hello World\n"; otherwise, what it does is
  57. >not defined (for instance, Microsoft C).
  58.  
  59. If MicroSoft C version x.y does not print "Hello World\n", MicroSoft C
  60. version x.y is broken.  (This would come as no surprise to anyone who
  61. has ever tried to use MicroSoft C for large projects.  Note that a
  62. broken compiler can still be useful.  The two are not entirely
  63. orthogonal, but neither are they mutually contradictory.)
  64.  
  65. >... you should be able to use:
  66. >
  67. >    char    *p;
  68. >
  69. >    p = mktemp( "/tmp/foo.XXXXXX");
  70. >
  71. >without ill effect.
  72.  
  73. Not so.  If the characters are kept in read-only storage, as permitted
  74. by the ANSI standard, this constitutes an attempt to modify a
  75. non-modifiable object.  The result is, according to the ANSI standard,
  76. undefined.  A bus error and core dump is a perfectly valid result.
  77.  
  78. The compiler cannot diagnose an error here because the type of a
  79. string literal is `array N of char', not `array N of read-only char',
  80. even when the individual `char's are read-only.  This is a consequence
  81. of ANSI C's type compatibility rules: if the type of the array were
  82. `const char [N]', code of the form:
  83.  
  84.     char *space = " ";
  85.  
  86. would require a diagnostic.  (This could have been avoided by adding
  87. special case rules, or by allowing `const char *q; p = q;', but the
  88. members of X3J11 did neither.)
  89.  
  90. GCC nevertheless allows you to obtain a warning anyway: the
  91. `-Wwrite-strings' option tells the compiler to use the const qualifier
  92. for string literal types.  This is not strictly conformant---although
  93. there is no way to tell, since compilers may issue bogus warnings at
  94. any time without violating the ANSI standard---and will, as the manual
  95. warns, merely be a nuisance unless you are *really* picky about using
  96. const qualifiers, but there it is.  (You might also want to use
  97. `-Wcast-qual'.)
  98. -- 
  99. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  100. Berkeley, CA        Domain:    torek@ee.lbl.gov
  101.