This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: BUG in function parameter passing ??????


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".


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]