[comp.sys.next] Whats wrong?

hoodr@syscube.csus.edu (Robert Hood) (11/29/89)

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

chari@nueces.cactus.org (Chris Whatley) (11/29/89)

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

-- 
Chris Whatley
Work: chari@pelican.ma.utexas.edu (NeXT Mail)		(512/471-7711 ext 123)
Play: chari@nueces.cactus.org (NeXT Mail)		(512/499-0475)
Also: chari@emx.utexas.edu

dd26+@andrew.cmu.edu (Douglas F. DeJulio) (11/29/89)

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.
-- 
Doug.deJ | dd26@andrew.cmu.edu             | Carnegie-Mellon University
******** | ...!harvard!andrew.cmu.edu!dd26 | Do not attend this college.

dtgcube (Edward Jung) (11/29/89)

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.

-- 
Edward Jung                             The Deep Thought Group, L.P.
BIX: ejung                                      3400 Swede Hill Road
NeXT or UNIX mail                                Clinton, WA.  98236
        UUCP: uunet!dtgcube!ed          Internet: ed@dtg.com

dennisg@kgw2.UUCP (Dennis Glatting) (11/29/89)

In article <1989Nov29.014513.21386@csusac.csus.edu>, hoodr@syscube.csus.edu (Robert Hood) writes:
> 
> 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')
> 


that doesn't supprise me.  your string is probably const data.  which means
its in a memory segment that can't be written to.  if i remember correctly
there is a switch on the compiler that says to make strings writable.



==+==+==+==+==+==+==+==+==+==+==+==+==+==
 ..umbc3.umbc.edu!tron!kgw2!dennisg  
      + Dennis P. Glatting
      + Xetron Corporation
      + Cincinnati, Ohio
      +  I want my own NeXT, 16 MB RAM,
      +    660 MB SCSI, NeXT Printer.
      +    Accepting Donations.
==+==+==+==+==+==+==+==+==+==+==+==+==+==

eht@f.word.cs.cmu.edu (Eric Thayer) (11/29/89)

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.
-- 
Eric H. Thayer      School of Computer Science, Carnegie Mellon
(412) 268-7679      5000 Forbes Ave, Pittsburgh, PA 15213