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

P

prakashsahni

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.
 
J

Jeff Flinn

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.

Welcome,

Jeff Flinn
 
P

P.J. Plauger

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
 
D

Daniel T.

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 { }
};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top