From: lmauro AT scientist DOT com (Leo Mauro) Subject: RE: BUG in function parameter passing ?????? 28 Aug 1998 07:08:54 -0700 Message-ID: <000001bdd23c$2212a580$1e6e31cf.cygnus.gnu-win32@leo-nt.rd.telesystech.com> References: <98082711213612 AT psicla DOT psi DOT ch> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit To: Cc: On Thursday, August 27, 1998 05:22 AM Mark DOT Koennecke AT psi DOT ch wrote: [SNIP] > /* why does this work? */ > strcpy(pBuffer,"Hello You"); > strtolower(pBuffer); > > /* but this gives a segmentation violation under Cygwin*/ > strtolower("Hello You"); [SNIP] Because a string constant is a constant, and a C compiler is free to allocate constants in read-only storage. It seems that the compiler you were originally using didn't care to do so. Try this much simpler piece of code: int main() { char *cp; cp = "asdfdgh"; *cp = 'x'; return 0; } It will also crash, at the *cp = 'x' statement. This is because cp gets set to point to the string constant, which (under Cygwin) is allocated in the read-only code segment. This same piece of code also crashes under my Linux (Pentium) box and my IRIX (SGI) box for the same exact reason (in these 2 systems the constant is allocated in a separate read-only segment instead of the code segment). Attempting to modify a constant is at best meaningless, and frequently leads to mysterious bugs. Beware. If you _really_ must modify a "constant" string, make it non- constant. For instance, use static char HelloYou[] = "Hello You"; to allocate the string in the regular data segment and give it a name. Then use strtolower(HelloYou); in you example. Leo Mauro Principal Scientist TeleSys Technologies, Inc. - 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".