This is the mail archive of the
mailing list for the Cygwin project.
RE: BUG in function parameter passing ??????
- To: <Mark dot Koennecke at psi dot ch>
- Subject: RE: BUG in function parameter passing ??????
- From: "Leo Mauro" <lmauro at scientist dot com>
- Date: Fri, 28 Aug 1998 00:27:14 -0400
- Cc: <gnu-win32 at cygnus dot com>
On Thursday, August 27, 1998 05:22 AM Mark.Koennecke@psi.ch wrote:
> /* why does this work? */
> strcpy(pBuffer,"Hello You");
> /* but this gives a segmentation violation under Cygwin*/
> strtolower("Hello You");
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:
cp = "asdfdgh";
*cp = 'x';
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
in you example.
TeleSys Technologies, Inc.
For help on using this list (especially unsubscribing), send a message to
"email@example.com" with one line of text: "help".