[cfe-dev] error with lambda function that results in endless loop

Ran Regev Ran.Regev at ecitele.com
Thu May 3 02:53:26 PDT 2012


Colleagues,

While working with some c++11 features I have encountered a problem with std::sort;
>From some reason it was stuck when I tried to sort more than 10 elements.

I simplified the code to use normal int, instead of my objects, and it gets stuck above 30 elements.

Here is the code, followed by the compilation command line, followed by the output, followed by more information:

12:30:06/auto> cat sortproblem.cpp

#include <iostream>
#include <vector>

void checkSort( std::vector<int>& v )
{
    std::sort( v.begin(), v.end(),
        [] (const int& a1, const int& a2) -> bool
        {
            return true;
        }
    );
}

int main()
{
    unsigned int MAX_ELEMENTS;

    MAX_ELEMENTS = 30;
    std::vector< int > v1;

    std::cout << "v1 created" << std::endl;

    for ( unsigned int i = 0; i < MAX_ELEMENTS; ++ i )
    {
        v1.push_back( i );
    }

    std::cout << "v1 filled" << std::endl;

    checkSort( v1 );

    std::cout << "v1 sorted" << std::endl;

    MAX_ELEMENTS = 31;
    std::vector< int > v2;

    std::cout << "v2 created" << std::endl;

    for ( unsigned int i = 0; i < MAX_ELEMENTS; ++ i )
    {
        v2.push_back( i );
    }

    std::cout << "v2 filled" << std::endl;

    checkSort( v2 );

    std::cout << "v2 sorted" << std::endl;
}

12:30:07/auto> ./build
------------------------------------ command line -----------------------------------------
/auto/tnd_cesr/users/rregev/c++11/clang/llvm/build/Debug+Asserts/bin/clang++
sortproblem.cpp
-o run
-std=c++11 -Wall -Wno-c++11-extensions -rpath /auto/tnd_cesr/users/rregev/c++11/clang/libcxx/build/lib -g -O0 -ferror-limit=3
-I ../../clang/libcxx/include/
-L/auto/tnd_cesr/users/rregev/c++11/clang/libcxx/build/lib
-lc++
------------------------------------ compilation starts here -------------------------------
------------------------------------ compilation ends here ---------------------------------
[sussita2]12:30:17/auto> ./run
v1 created
v1 filled
v1 sorted
v2 created
v2 filled



------------------ findings ------------------
A)
This is the line that loops-forever:
0x0000000000401ef6 in std::__1::__sort<<lambda at sortproblem.cpp:8:9> &, int *>(int *, int *, class {...} &) (__first=0x605160, __last=0x6051dc, __comp=...)
    at ../../clang/libcxx/include/algorithm:3616
3616                    while (__comp(*__i, *__m))

B)
When changing the lambda function into something more meaningful than just returning true the problem was solved.
    std::sort( v.begin(), v.end(),
        [] (const int& a1, const int& a2) -> bool
        {
            return a1 < a2;
        }
    );
Surly returning true regardless of the value of the elements has no real meaning and can results in nonsense like a1<a2==true && a2<a1==true
And surly this is a bad idea to write code like this, but still - why it passes 30 and not 31?


Ran.



This e-mail message is intended for the recipient only and contains information which is CONFIDENTIAL and which may be proprietary to ECI Telecom. If you have received this transmission in error, please inform us by e-mail, phone or fax, and then delete the original and all copies thereof.





More information about the cfe-dev mailing list