home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11528 < prev    next >
Encoding:
Text File  |  1992-07-23  |  2.1 KB  |  57 lines

  1. Path: sparky!uunet!dtix!darwin.sura.net!mips!think.com!barmar
  2. From: barmar@think.com (Barry Margolin)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Strange dicrepancy between  DECstation & Sun 4
  5. Message-ID: <14n19bINNt58@early-bird.think.com>
  6. Date: 23 Jul 92 19:28:43 GMT
  7. References: <BruoII.21n@cs.dal.ca>
  8. Organization: Thinking Machines Corporation, Cambridge MA, USA
  9. Lines: 45
  10. NNTP-Posting-Host: telecaster.think.com
  11.  
  12. In article <BruoII.21n@cs.dal.ca> vanadis@cs.dal.ca (Jose Castejon-Amenedo) writes:
  13. [Extracting the relevant part of his program:]
  14. >main (int argc, char ** argv)
  15. >{
  16. >/* argv points to an array of strings. */
  17. >
  18. >  char * strs[] = { "points", "Herewith", "shall", "we", "show",
  19. >                    "our", "third", "test" } ;
  20. ...
  21. >  printf ("%c\t", ++**argv);   /* Pointer unchanged.
  22. >                                  Replaces 1st character 3rd string by
  23. >                                  next character in character table,
  24. >                                  and then returns that. */
  25.  
  26. On the Sun this gets a segmentation fault, while it works on A Decstation.
  27.  
  28. This is a bug in your code.  On many systems, literal strings are stored in
  29. the read-only text section of the program.  Attempting to modify them can
  30. have unpredictable effects.  Even if it doesn't cause an immediate trap, it
  31. may have strange effects on your program behavior; for instance, if there
  32. are two identical literals, the compiler might create a single constant and
  33. use a pointer to it for both, e.g. after declaring
  34.  
  35. char *foo = "Test";
  36. char *bar = "Test";
  37.  
  38. it would be possible for (foo == bar) to be true, and the following might
  39. print "Best":
  40.  
  41.     *foo = 'B';
  42.     printf ("%s", bar);
  43.  
  44. With GCC, you can tell it not to put strings in read-only storage by using
  45. the -fwritable-strings option.
  46.  
  47. P.S. It would be nice if you'd reduce example programs to the minimum that
  48. exhibits your problem.  Following all those extraneous ++'s, and the
  49. totally irrelevant references to argv, was difficult when I was trying to
  50. figure out exactly which line was having the problem.  Luckily you
  51. commented it well.
  52. -- 
  53. Barry Margolin
  54. System Manager, Thinking Machines Corp.
  55.  
  56. barmar@think.com          {uunet,harvard}!think!barmar
  57.