OT: Re: BUG in function parameter passing ??????
Fri Aug 28 06:34:00 GMT 1998
I know this isn't helpfull, but I'm fresh out of Sedgewick's algorithms
class (CS226) and I can't resist pointing out that your strtolower
function is quadratic, since it calls strlen on every iteration ... I'll
post a corrected version, which I'm (almost) sure someone else will
strike down ...
As to your problem, I don't know the specific answer, but your code
relies on the fact that the compiler puts the string "Hello You"
somewhere writeable. This may be a "reasonable" assumption, but you
still shouldn't make it. As you have found, the complier is not always
"reasonable" from one's point of view. From my (albeit only 7 years or
so) of experience, what you've done just looks fishy -- ie. it relies on
compiler behavior that's in (serious) doubt. Others can quote the actual
specs if they wish ... but from what I remember, such strings are
*supposed* to be read only.
Take a look at these results, from a SunOS box:
yuma:~/scratch> cc temp.c
yuma:~/scratch> gcc temp.c
Segmentation Fault (core dumped)
A snippet from the assembly shows (persumably) that "Hello You" is in a
read only section under gcc; i didn't look, but it's probably in bss or
common or something with cc. Whatever ... live with it.
.asciz "Hello You"
> void strtolower(char *pText)
// i gets strlen(pText) after we assert(pText)
int i = ( assert(pText) , strlen(pText) );
while( i-- ) // iteratre over string
*pText = tolower(*pText++); // flip each char to lowercase
> int main(int argc, char *argv)
> char pBuffer;
> /* why does this work? */
> strcpy(pBuffer,"Hello You");
> /* but this gives a segmentation violation under Cygwin*/
> strtolower("Hello You");
> For help on using this list (especially unsubscribing), send a message to
> "firstname.lastname@example.org" with one line of text: "help".
Matthew Moskewicz | mailto:moskewcz@Princeton.edu
234 Lockhart | http://www.princeton.edu/~moskewcz
Princeton, NJ 08544 | (609)258-UNKN (till september)
For help on using this list (especially unsubscribing), send a message to
"email@example.com" with one line of text: "help".
More information about the Cygwin