ftp.nice.ch/peanuts/GeneralData/Usenet/news/1989/CSN-89.tar.gz#/comp-sys-next/1989/Nov/Whats-wrong?

This is Whats-wrong? in view mode; [Up]


Date: Sun 29-Nov-1989 01:45:13 From: Unknown Subject: Whats wrong? I ran into a little problem. I have this piece of code that works on all the Unix boxes I could try, *EXCEPT* the NeXT: #include <stdio.h> struct test { char *temp; int temp2; }; struct test demo = { "this are a test", 4}; main () { puts (demo.temp); *demo.temp = '*'; puts (demo.temp); } Its a very simple piece of code that replaces the first character with an '*'. It produces: this are a test Bus error Why? Am I missing a compiler option somewhere? (I just used 'cc test.c') ------------------------------------------------------------------------------- Robert Hood -- California State University: Sacramento INTERNET: hoodr@csus.edu BITNET: hoodr@CALSTATE UUCP: ...!ucdavis!cssexb!hoodr or ...!uunet!mmsac!csusac!cssexb!hoodr >From: chari@nueces.cactus.org (Chris Whatley)
Date: Sun 29-Nov-1989 07:28:55 From: Unknown Subject: Re: Whats wrong? hoodr@syscube.csus.edu (Robert Hood) writes: >I ran into a little problem. I have this piece of code that works on all >the Unix boxes I could try, *EXCEPT* the NeXT: >struct test demo = { "this are a test", 4}; >main () > { > puts (demo.temp); > *demo.temp = '*'; > puts (demo.temp); > } >this are a test >Bus error >Why? Am I missing a compiler option somewhere? (I just used 'cc test.c') Yes you are. You need the "fwritable-strings" option to gcc to allow you to alter a string constant like you have above. This is a very useful flag when you are porting since this is such a common practice. Chris
Date: Sun 29-Nov-1989 08:46:14 From: Unknown Subject: Re: Whats wrong? hoodr@syscube.csus.edu (Robert Hood) writes: > I ran into a little problem. I have this piece of code that works on all > the Unix boxes I could try, *EXCEPT* the NeXT: [...] > Its a very simple piece of code that replaces the first character with an > '*'. It produces: > > this are a test > Bus error This is occuring becuase gcc by default does not let you write into strings that started as constants. If you give the compiler the "-fwritable-strings" switch it should work as you expect.
Date: Sun 29-Nov-1989 13:48:01 From: Unknown Subject: Re: Whats wrong? In article <1989Nov29.014513.21386@csusac.csus.edu> hoodr@syscube.csus.edu (Robert Hood) writes: > >struct test demo = { "this are a test", 4}; > *demo.temp = '*'; >Bus error > Did you compile with -fwritable-strings? Strictly speaking, you are trying to change a string constant.
Date: Sun 29-Nov-1989 10:19:23 From: Unknown Subject: Re: Whats wrong? In article <1989Nov29.014513.21386@csusac.csus.edu> hoodr@syscube.csus.edu (Robert Hood) writes: I ran into a little problem. I have this piece of code that works on all the Unix boxes I could try, *EXCEPT* the NeXT: [source code and other stuff deleted] Why? Am I missing a compiler option somewhere? (I just used 'cc test.c') Gnu's C compiler on the NeXT machine by default does not allow writable strings for string constants (which is what you declare implicitly by using the quotes). When you initialize a string from a constant, the string points to an unwritable area of memory. This is because the compiler will unique all the strings it finds to save initializing string storage space. To excerpt from the gcc docs: * GNU CC normally makes string constants read-only. If several identical-looking string constants are used, GNU CC stores only one copy of the string. One consequence is that you cannot call `mktemp' with a string constant argument. The function `mktemp' always alters the string its argument points to. Another consequence is that `sscanf' does not work on some systems when passed a string constant as its format control string. This is because `sscanf' incorrectly tries to write into the string constant. Likewise `fscanf' and `scanf'. The best solution to these problems is to change the program to use `char'-array variables with initialization strings for these purposes instead of string constants. But if this is not possible, you can use the `-fwritable-strings' flag, which directs GNU CC to handle string constants the same way most C compilers do. So you can either set a compiler flag or change your declaration. Note that the traditional practice is really a bad one.

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Marcel Waldvogel and Netfuture.ch.