469,628 Members | 1,231 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,628 developers. It's quick & easy.

[STL] Segmentation fault on using std::sort

Hello

I am new to template programming, so i tried the 'example' from
http://www.sgi.com/tech/stl/functors.html.
I can compile the code but when i want to run the program
I get a segmentation fault when the part of std::sort(...) is
reached, but I don't understand why.
------------ Code start ---------------------

#include <iostream>
#include <vector>

struct less : public std::binary_function<double, double, bool> {
bool operator()(double x, double y) { return x < y; }
};

int main(){
std::vector<double> V(50);

std::generate(V.begin(), V.end(), rand);

std::sort(V.begin(), V.end(), less());
}

--------------- Code end -----------------------

thanks in advance

Manfred

PS: I am using linux and gcc 3.4.4 but this shouldn't be the problem
afaik
Jul 23 '05 #1
8 3952


Manfred wrote:
Hello

I am new to template programming, so i tried the 'example' from
http://www.sgi.com/tech/stl/functors.html.
I can compile the code but when i want to run the program
I get a segmentation fault when the part of std::sort(...) is
reached, but I don't understand why.

[snip]

PS: I am using linux and gcc 3.4.4 but this shouldn't be the problem
afaik


i just tried running your program and it runs fine! i use gcc 3.3.4

Jul 23 '05 #2
Manfred <ne******@web.de> wrote:
I can compile the code but when i want to run the program
I get a segmentation fault when the part of std::sort(...) is
reached, but I don't understand why.

#include <iostream>
#include <vector>

struct less : public std::binary_function<double, double, bool> {
bool operator()(double x, double y) { return x < y; }
};

int main(){
std::vector<double> V(50);
std::generate(V.begin(), V.end,rand
std::sort(V.begin(), V.end,less
}

I doubt that this code compiles. You're missing some parentheses after
function and method/constructor calls. After adding them everything should
work fine. Maybe you can post the actual (compiling) code you're using.

Martin
Jul 23 '05 #3
Martin Gieseking wrote:
Manfred <ne******@web.de> wrote:

I can compile the code but when i want to run the program
I get a segmentation fault when the part of std::sort(...) is
reached, but I don't understand why.

#include <iostream>
#include <vector>

struct less : public std::binary_function<double, double, bool> {
bool operator()(double x, double y) { return x < y; }
};

int main(){
std::vector<double> V(50);
std::generate(V.begin(), V.end,rand
std::sort(V.begin(), V.end,less
}


I doubt that this code compiles. You're missing some parentheses after
function and method/constructor calls. After adding them everything should
work fine. Maybe you can post the actual (compiling) code you're using.


Huh?!

Your newsreader must have screwed up what i posted.
But here once again:

------------ Code start ---------------------

#include <iostream>
#include <vector>

struct less : public std::binary_function<double, double, bool> {
bool operator()(double x, double y) { return x < y; }
};

int main(){
std::vector<double> V(50);

std::generate(V.begin(), V.end(), rand);

std::sort(V.begin(), V.end(), less());
}
--------------- Code end -----------------------

Manfred
Jul 23 '05 #4
forayer wrote:

Manfred wrote:
Hello

I am new to template programming, so i tried the 'example' from
http://www.sgi.com/tech/stl/functors.html.
I can compile the code but when i want to run the program
I get a segmentation fault when the part of std::sort(...) is
reached, but I don't understand why.

[snip]

PS: I am using linux and gcc 3.4.4 but this shouldn't be the problem
afaik

i just tried running your program and it runs fine! i use gcc 3.3.4


Thanks I tried gcc 3.3.4 and it works :)
Strange thing. Maybe my gcc or libstdc++ is damaged. :(

Manfred
Jul 23 '05 #5


Manfred wrote:
[snip]
Thanks I tried gcc 3.3.4 and it works :)
Strange thing. Maybe my gcc or libstdc++ is damaged. :(


the latest stl files can be downloaded from sgi. do a 'gcc -v' to check
where gcc looks for the c++ includes.

cheers,
forayer

Jul 23 '05 #6
forayer wrote:
the latest stl files can be downloaded from sgi. do a 'gcc -v' to check
where gcc looks for the c++ includes.

cheers,
forayer


I have the newest g++ includes, because i built gcc 3.4.4 and 3.3.4 from
sources. I'll ask for it in the forums of my distro.

Thanks anyway

Manfred
Jul 23 '05 #7
Martin Gieseking <mg******@uos.de> wrote:
Manfred <ne******@web.de> wrote:
I can compile the code but when i want to run the program
I get a segmentation fault when the part of std::sort(...) is
reached, but I don't understand why.

#include <iostream>
#include <vector>

struct less : public std::binary_function<double, double, bool> {
bool operator()(double x, double y) { return x < y; }
};

int main(){
std::vector<double> V(50);
std::generate(V.begin(), V.end,rand
std::sort(V.begin(), V.end,less
}


I doubt that this code compiles. You're missing some parentheses after
function and method/constructor calls. After adding them everything should
work fine. Maybe you can post the actual (compiling) code you're using.


Are you using an older version of ZoneAlarm, by chance?

See <http://tinyurl.com/8axvj>.
Jul 23 '05 #8
Thomas Edison <fo*@bar.invalid> wrote:
Are you using an older version of ZoneAlarm, by chance?

See <http://tinyurl.com/8axvj>.


By the way, the new Google Groups seems to be FUBAR (at least on here) --
so the messages in that thread keep repeating and repeating and
repeating...
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Aaron Broad | last post: by
10 posts views Thread by Paul Schneider | last post: by
6 posts views Thread by Der Andere | last post: by
7 posts views Thread by Stuart | last post: by
7 posts views Thread by yinglcs | last post: by
1 post views Thread by Varun Kacholia | last post: by
8 posts views Thread by markww | last post: by
11 posts views Thread by Magcialking | last post: by
5 posts views Thread by feverzsj | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.