Suppose that someone wants to see if a string is shorter than another and
writes:
   #include <string.h>
   bool f(char const * const a, char const * const b) {return strlen(a) <=
strlen(b);}

Then g++ generates this:
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ebx
        subl    $4, %esp
        movl    8(%ebp), %eax
        movl    %eax, (%esp)
        call    strlen
        movl    %eax, %ebx
        movl    12(%ebp), %eax
        movl    %eax, (%esp)
        call    strlen
        cmpl    %eax, %ebx
        setbe   %al
        addl    $4, %esp
        popl    %ebx
        popl    %ebp
        ret

But it looks like this code reads through both strings completely! This is
obviously very efficient if one of the strings is much longer than the other.
It should of course only read as far into each string as the shortest of them
is long. The generated code should be similar to what this would give:
bool g(char const * a, char const * b) {
   for (;; ++a, ++b)
      if      (not *a)
         return true;
      else if (not *b)
         return false;
}


-- 
           Summary: comparing lengths of 2 strings reads through both
                    strings completely
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: esigra at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38986

Reply via email to