473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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 '06 #1
4 4495
pr**********@gmail.com 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
Apr 28 '06 #2
<pr**********@gmail.com> wrote in message
news:11********************@g10g2000cwb.googlegrou ps.com...
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
Apr 28 '06 #3
In article <11********************@g10g2000cwb.googlegroups.c om>,
pr**********@gmail.com 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 { }
};
Apr 28 '06 #4
Thanks, guys, you nailed it.

Apr 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
6
by: alexhong2001 | last post by:
Does "std::sort" work only with sequence containers, not associative containers at all? Among sequential containers, can it be used with "list", "queue" and other sequence containers besides...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
11
by: Jeff Schwab | last post by:
Would std::sort ever compare an object with itself? I'm not talking about two distinct, equal-valued objects, but rather this == &that. The container being sorted is a std::vector. I've never...
1
by: Markus Dehmann | last post by:
In the following code example, I define several Comparator classes which contain different compare functions to use with std::sort. I have a Sorter class that gets passed a Comparator and is...
10
by: ikarus | last post by:
Hello C++ Gurus! I'm comparing sorting algorithm for study goals. I've compared STL std::sort and hand-coded introsort on millions (tens of millions) of integers array sorting. It was tested...
5
by: Ganesh | last post by:
hi ! Can the predicate function used in std::sort take optional arguments ? For instance. I have a class Point. I create a vector of this and then want to compare the slopes of these points with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.