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