Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ String Compare

Name: Anonymous 2011-09-21 16:58

Sup niggaz. This shit is throwing an exception when it gets to this code in runtime. Any ideas?

- Assume "in" is a string comprised of "http://google.com/derp"
- found is a link found, can be w/e string

As far as I can tell, it explodes on the line where I'm trying to compare the 2 strings, as for some reason the loop is not concatenating the compare string - any idea why that is?


bool isLinkExternal(string in, string found)
{
    string compare = "";
    bool isExternal = false;

    //Get domain of original link
    for (int i = 6; in[i] != '/' ; i++)
    {
        compare += in[i];
    }
   
    //Compare domains of original and found links
    if (found.compare(7,compare.length(),compare) == 0)
        isExternal = true;

    return isExternal;
}


The way I see it, the loop starts at 7 chars in, which excludes the initial "http://". It should then keep going, concatenating each char into the compare string until a slash is found. The last bit is to compare that string to the portion of the string "in" of the same length, starting at the same point (7 chars in, for length of compare)

Name: Anonymous 2011-09-22 3:03

>>17
Further, you might ask "is there really any difference?"

Here is how your version of the function starts:
00000004         push    0FFFFFFFFh
00000006         push    offset $L8902
0000000B         mov     eax, dword ptr fs:__except_list
00000011         push    eax
00000012         mov     dword ptr fs:__except_list, esp
00000019         sub     esp, 14h
0000001C         push    ebx
0000001D         push    ebp
0000001E         push    esi
0000001F         push    edi
00000020         mov     al, [esp+30h+arg_10]
00000024         mov     edi, [esp+30h+arg_18]
00000028         xor     ebx, ebx
0000002A         lea     ecx, [esp+30h+var_1C]
0000002E         push    ebx
0000002F         mov     [esp+34h+var_4], 1
00000037         xor     ebp, ebp
00000039         mov     [esp+34h+var_1C], al
0000003D         call    std::basic_string<char,std::char_traits<char>,std::allocator<char>>::_Tidy(bool)
00000042         xor     esi, esi
00000044         cmp     edi, ebx
00000046         mov     byte ptr [esp+30h+var_4], 2
0000004B         jz      short loc_0_BD
0000004D         mov     ecx, [esp+30h+arg_14]
00000051 loc_0_51:
00000051         cmp     [esp+30h+arg_18], esi
00000055         jb      short loc_0_6D
00000057         cmp     ecx, ebx
00000059         jz      short loc_0_6D
0000005B         lea     ecx, [esp+30h+arg_10]
0000005F         call    std::basic_string<char,std::char_traits<char>,std::allocator<char>>::_Freeze(void)
00000064         mov     ecx, [esp+30h+arg_14]
00000068         lea     eax, [ecx+esi]
0000006B         jmp     short loc_0_72

...and on and on, for 420 bytes in this function. That's not all either, there's a bunch of other functions that bring the total to over 1600 bytes.

So you say "1.6KB is nothing these days"... let's compare with my version:

00000000         push    esi
00000001         push    edi
00000002         mov     edi, [esp+arg_0]
00000006         push    2Fh ; '/'
00000008         lea     esi, [edi+7]
0000000B         push    esi
0000000C         call    _strchr
00000011         sub     eax, edi
00000013         push    eax
00000014         mov     eax, [esp+0Ch+arg_4]
00000018         add     eax, 7
0000001B         push    eax
0000001C         push    esi
0000001D         call    _strncmp
00000022         add     esp, 14h
00000025         neg     eax
00000027         sbb     eax, eax
00000029         pop     edi
0000002A         inc     eax
0000002B         pop     esi
0000002C         retn


That's it. 44 bytes. We both essentially wrote code to do the same thing, but you ended up needing 68 times the memory to do it (and that's not including the allocation of the extra string in your version.)

"is there really any difference?" See for yourself.

I hope this little example has given you an appreciation for what can happen if you aren't careful with how you design programs.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List