std::sort crashes if more than 8 elements to be sorted.

Discussion in 'C++' started by prakashsahni@gmail.com, Apr 28, 2006.

  1. Guest

    I am using a sort func object like
    struct mystruct {
    bool operator () (MyClass* const &a, MyClass* const&b) {};
    }

    Invoke it like
    std::sort(vec.begin(), vec.end(),mystruct);

    Where vec is an std::vector<MyClass*>.

    Everything works fine, except when my vector has more than 8 elements,
    there is SEGV inside STL.
    I am using Linux64(opteron), linux32(intel). gcc compiler
    gcc/v3.2.3p1/bin/g++.
    Is this a known issue/ bug , any way to fix this ?

    Thanks.
     
    , Apr 28, 2006
    #1
    1. Advertising

  2. Jeff Flinn Guest

    wrote:
    > I am using a sort func object like
    > struct mystruct {
    > bool operator () (MyClass* const &a, MyClass* const&b) {};


    The bug is no doubt in what you left out of the empty braces above. Your
    function object must provide for strict-weak-ordering. Google it.

    > }
    >
    > Invoke it like
    > std::sort(vec.begin(), vec.end(),mystruct);
    >
    > Where vec is an std::vector<MyClass*>.
    >
    > Everything works fine, except when my vector has more than 8 elements,
    > there is SEGV inside STL.
    > I am using Linux64(opteron), linux32(intel). gcc compiler
    > gcc/v3.2.3p1/bin/g++.
    > Is this a known issue/ bug , any way to fix this ?


    See above.

    > Thanks.


    Welcome,

    Jeff Flinn
     
    Jeff Flinn, Apr 28, 2006
    #2
    1. Advertising

  3. P.J. Plauger Guest

    <> wrote in message
    news:...

    >I am using a sort func object like
    > struct mystruct {
    > bool operator () (MyClass* const &a, MyClass* const&b) {};
    > }
    >
    > Invoke it like
    > std::sort(vec.begin(), vec.end(),mystruct);
    >
    > Where vec is an std::vector<MyClass*>.
    >
    > Everything works fine, except when my vector has more than 8 elements,
    > there is SEGV inside STL.
    > I am using Linux64(opteron), linux32(intel). gcc compiler
    > gcc/v3.2.3p1/bin/g++.
    > Is this a known issue/ bug , any way to fix this ?


    My bet is your mystruct object does not impose a strict weak ordering
    on Myclass objects. Any chance that (a < b && b < a) can ever be true
    for two elements? If so, you're hosed.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
     
    P.J. Plauger, Apr 28, 2006
    #3
  4. Daniel T. Guest

    In article <>,
    wrote:

    > I am using a sort func object like
    > struct mystruct {
    > bool operator () (MyClass* const &a, MyClass* const&b) {};
    > }
    >
    > Invoke it like
    > std::sort(vec.begin(), vec.end(),mystruct);
    >
    > Where vec is an std::vector<MyClass*>.
    >
    > Everything works fine, except when my vector has more than 8 elements,
    > there is SEGV inside STL.
    > I am using Linux64(opteron), linux32(intel). gcc compiler
    > gcc/v3.2.3p1/bin/g++.
    > Is this a known issue/ bug , any way to fix this ?


    There is nothing wrong with either vector or sort. Either you are doing
    something wrong in your 'mystruct' or you are doing something wrong in
    your "MyClass", or you are doing something wrong before the sort
    function is called.

    BTW you should be invoking it like:

    std::sort( vec.begin(), vec.end(), mystruct() );

    and use a sort func object like:

    struct mystruct : std::binary_function< MyClass*, MyClass*, bool >
    {
    bool operator()( MyClass* a, MyClass* b ) const { }
    };
     
    Daniel T., Apr 28, 2006
    #4
  5. Guest

    Thanks, guys, you nailed it.
     
    , Apr 28, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    496
    Marcus Kwok
    Apr 6, 2007
  2. desktop

    Sort elements in a std::list?

    desktop, Apr 7, 2008, in forum: C++
    Replies:
    10
    Views:
    1,064
    Jerry Coffin
    Apr 10, 2008
  3. Navin
    Replies:
    1
    Views:
    728
    Ken Schaefer
    Sep 9, 2003
  4. Steven D'Aprano
    Replies:
    0
    Views:
    116
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    98
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page