473,326 Members | 2,732 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,326 software developers and data experts.

qsort() and std::vector

I've tried to use C qsort() on an object derivate by std::vector, but
it doesn't work.

I've the follow structure:

struct Item {
std::string name;
int number;
}
And the class "Container":

class Container : public std::vector<Item{ /* ... */ }

sort method is the follow:

void Container::sort()
{
qsort(this, this->size(), sizeof(Item), compare_items_by_name);
}

compare function is:

int compare_items_by_name(const void* p, const void* q)
{
return static_cast<const
Item*>(p)->name.compare(static_cast<const Item*>(q)->name);
}

My compiler (g++ 4.1) doesn't give warnings nor errors, but:

Program received signal SIGSEGV, Segmentation fault.
0xb7ee700e in std::string::compare () from /usr/lib/libstdc++.so.6

I can't find the mistake, maybe in the casting?

Thanks a lot!

s.

Jan 20 '07 #1
4 15457
gallows wrote:
I've tried to use C qsort() on an object derivate by std::vector, but
it doesn't work.
It's not a good idea, probably, anyway.
I've the follow structure:

struct Item {
std::string name;
int number;
}
;
>
And the class "Container":

class Container : public std::vector<Item{ /* ... */ }

sort method is the follow:

void Container::sort()
{
qsort(this, this->size(), sizeof(Item), compare_items_by_name);
}

compare function is:

int compare_items_by_name(const void* p, const void* q)
{
return static_cast<const
Item*>(p)->name.compare(static_cast<const Item*>(q)->name);
}

My compiler (g++ 4.1) doesn't give warnings nor errors, but:

Program received signal SIGSEGV, Segmentation fault.
0xb7ee700e in std::string::compare () from /usr/lib/libstdc++.so.6

I can't find the mistake, maybe in the casting?
The mistake is that 'qsort' uses whatever implementation-defined
mechanism for swapping the two values it thinks are out of order.
It only works with POD. Since your 'Item' has 'std::string' as
a member, it's not a POD.

Use 'std::sort'. It's just as quick as 'qsort' and is much, much
better when it comes to containers.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 20 '07 #2

"gallows" <g4*****@gmail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
I've tried to use C qsort() on an object derivate by std::vector, but
it doesn't work.

I've the follow structure:

struct Item {
std::string name;
int number;
}
And the class "Container":

class Container : public std::vector<Item{ /* ... */ }
Try to avoid inheriting from standard containers.
They don't have virtual destructors. For what you're
showing, containment will do.
>
sort method is the follow:

void Container::sort()
{
qsort(this, this->size(), sizeof(Item), compare_items_by_name);
}

compare function is:

int compare_items_by_name(const void* p, const void* q)
{
return static_cast<const
Item*>(p)->name.compare(static_cast<const Item*>(q)->name);
}

My compiler (g++ 4.1) doesn't give warnings nor errors, but:

Program received signal SIGSEGV, Segmentation fault.
0xb7ee700e in std::string::compare () from /usr/lib/libstdc++.so.6

I can't find the mistake, maybe in the casting?

Thanks a lot!
Don't use 'qsort()'. Use 'std::sort'

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

struct Item
{
Item(const std::string& n, int i)
: name(n), number(i) { }

std::string name;
int number;
};

bool comp(const Item& I1, const Item& I2)
{
return I1.name < I2.name;
}

class Container
{
std::vector<Item v;
public:
void sort()
{
std::sort(v.begin(), v.end(), comp);
}

void append(const Item& i)
{
v.push_back(i);
}

void show(const std::string& prefix = "") const
{
std::cout << prefix;
std::vector<Item>::const_iterator it(v.begin());
std::vector<Item>::const_iterator en(v.end());
for(; it != en; ++it)
std::cout << it->name << '\t' << it->number << '\n';
}
};

int main()
{
Container c;
c.append(Item("XYZ", 42));
c.append(Item("ABC", 25));
c.append(Item("JKL", 10));
c.append(Item("PQR", 6));
c.append(Item("DEF", 0));

c.show("unsorted:\n");
c.sort();
c.show("sorted:\n");

return 0;
}

-Mike

Jan 20 '07 #3
Mike Wahler ha scritto:
>
Try to avoid inheriting from standard containers.
They don't have virtual destructors. For what you're
showing, containment will do.
Ok, I will do.
>
Don't use 'qsort()'. Use 'std::sort'

-- cut --

-Mike
Oh thanks, it's perfect. Understood.

Jan 21 '07 #4
Victor Bazarov <v.********@comacast.netwrote:
Use 'std::sort'. It's just as quick as 'qsort'
Even better, std::sort may be faster than qsort due to better inlining
of the comparison function.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jan 22 '07 #5

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

Similar topics

34
by: richard | last post by:
What might cause qsort to crash? I'm using qsort to sort a few thousand items of a not too complex structure. Tried it with one data set - worked fine. Tried another similarly sized data set...
11
by: William Buch | last post by:
I have a strange problem. The code isn't written by me, but uses the qsort function in stdlib. ALWAYS, the fourth time through, the memory location of variable list (i.e. mem location = 41813698)...
5
by: Steve | last post by:
can someone tell me how qsort function in <stdlib.h> is used (qsort(..........))? the function has a buffer, two void * parameters and the a pointer to a compare function. Thanks.
7
by: Excluded_Middle | last post by:
Suppose I have a struct typdef struct foo { int age; char *name; }foo; now I made a list of foo using
17
by: Trent Buck | last post by:
The fourth argument is a comparator that returns `an integer less than, equal to, or greater than zero' depending on the ordering of its arguments. If I don't care about the order and simply...
32
by: John Smith | last post by:
I'm trying to figure out qsort(). I haven't seen any practical examples, only synopsis. In the code below, the array is not sorted. Can someone give me some help? #include <stdio.h> #include...
3
by: No Such Luck | last post by:
Hi All: The code below (using the qsort function) produces the following incorrect result. The last two numbers are not sorted. It this innaccurate result specific to my compiler's qsort, or is...
9
by: yogeshmk | last post by:
I'm trying to write a program which sorts the strings entered by user. I run it as $./srt -sa dddd a ccc bb # -sa is options to the program s-string a-ascending and expect a bb ccc
10
by: gauss010 | last post by:
Suppose I have an object A of type char. Each A is a buffer containing a string, and I want to sort the M strings of A using the strcmp function. The description of the qsort function says that I...
61
by: Ron Ford | last post by:
K&R has three different versions of qsort, and the ultimate one is supposed to be like the one in the std library. I'm trying to implement the first, which is in §4.10. I think I'm pretty close...
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.