Subject: RE: BUG in function parameter passing ??????
From: [email-masked] (Leo Mauro)

28 Aug 1998 07:08:54 -0700
Message-ID: <[email-masked]>
References: <[email-masked]>
Mime-Version: 1.0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
To: <[email-masked]>
Cc: <[email-masked]>

On Thursday, August 27, 1998 05:22 AM [email-masked] 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
"[email-masked]" with one line of text: "help".