delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1998/08/29/00:58:13

From: jsturm AT sigma6 DOT com (Jeff Sturm)
Subject: Re: BUG in function parameter passing ??????
29 Aug 1998 00:58:13 -0700 :
Message-ID: <35E6BE10.12F2CBD9.cygnus.gnu-win32@sigma6.com>
References: <98082711213612 AT psicla DOT psi DOT ch>
Mime-Version: 1.0
To: Charles G Waldman <cgw AT pgt DOT com>
Cc: Mark DOT Koennecke AT psi DOT ch, gnu-win32 AT cygnus DOT com

Charles G Waldman wrote:
> 
> Mark DOT Koennecke AT psi DOT ch writes:
>  >
>  > int main(int argc, char *argv[])
>  > {
>  >   char pBuffer[132];
>  >
>  >   /* why does this work? */
>  >   strcpy(pBuffer,"Hello You");
>  >   strtolower(pBuffer);
>  >
>  >   /* but this gives a segmentation violation under Cygwin*/
>  >   strtolower("Hello You");
>  >
> 
> Because the string "Hello You" is a constant, and is allocated in the
> read-only segment of the executable.  The buffer pBuffer is allocated
> read/write.   This is done so that storage for literal strings can be
> shared between object files.

It's also for sharing string constants within an object file.  If you
compile the example with -fwritable-strings the constant "Hello You"
will appear twice, otherwise just once.

Modifying string constants is bad programming style and forbidden by the
C spec, I think.  I don't understand why gcc -Wall doesn't emit a
warning when assigning a string constant to a non-constant parameter. 
This code fragment will emit a warning on the 2nd call to strtolower(),
but no the first:

  int main(void) {
    const char *c = "Hello You";
    strtolower("Hello You");    /* no warning */
    strtolower(c);              /* gcc warns here */
  }

-- 
Jeff Sturm
jsturm AT sigma6 DOT com
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019