BUG in function parameter passing ??????
Leo Mauro
lmauro@scientist.com
Fri Aug 28 00:39:00 GMT 1998
On Thursday, August 27, 1998 05:22 AM Mark.Koennecke@psi.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@cygnus.com" with one line of text: "help".
More information about the Cygwin
mailing list