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]

OT: Re: BUG in function parameter passing ??????


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:

[begin test]
yuma:~/scratch> cc temp.c
yuma:~/scratch> a.out
Success
yuma:~/scratch> gcc temp.c
yuma:~/scratch> a.out
Segmentation Fault (core dumped)
yuma:~/scratch> 

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.

.section        ".rodata"
        .align 8
.LLC3:
        .asciz  "Hello You"
        .align 8  
.LLC4:
        .asciz  "Success\n"
.section        ".text"  


Mark.Koennecke@psi.ch wrote:
> 
>   void strtolower(char *pText)
>   {
[snip]
      // 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
    }
[resume]
> /*---------------------------------------------------------------------*/
> 
> int main(int argc, char *argv[])
> {
>   char pBuffer[132];
> 
>   /* why does this work? */
>   strcpy(pBuffer,"Hello You");
>   strtolower(pBuffer);
> 
>   /* but this gives a segmentation violation under Cygwin*/
>   strtolower("Hello You");
> 
>   printf("Success\n");
> 
> }
> -
> For help on using this list (especially unsubscribing), send a message to
> "gnu-win32-request@cygnus.com" 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
"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]