I have following simple code:
#include <cstdio>
#include <queue>
#include <iostream>
struct Pacient {
int ill_state;
int ev_num;
bool operator==(const Pacient& other) const {
return ill_state == other.ill_state && ev_num == other.ev_num;
}
bool operator<(const Pacient& other) const {
return (ill_state < other.ill_state) || (ill_state == other.ill_state && ev_num > other.ev_num); // má menšiu prioritu, ak čaká kratšie (vyššie číslo na kartičke pri vstupe do ambulancie
}
bool operator>(const Pacient& other) const {
return (ill_state > other.ill_state) || (ill_state == other.ill_state && ev_num < other.ev_num);
}
};
int main() {
char* ccmd;
std::priority_queue<Pacient> ps;
int ev_num, ill_state;
while (std::scanf("%s", ccmd)) {
std::string cmd(ccmd);
if (cmd == "dalsi") {
if (ps.empty()) {
std::printf("-1\n");
} else {
std::printf("%d\n", ps.top().ev_num);
ps.pop();
}
} else if (cmd == "pacient") {
std::scanf("%d%d\n", &ev_num, &ill_state);
Pacient new_ps;
new_ps.ev_num = ev_num;
new_ps.ill_state = ill_state;
ps.push(new_ps);
} else if (cmd == "koniec") {
break;
}
}
return 0;
}
After compiling and entering something to stdin, I have got following segfault:
Program received signal SIGSEGV, Segmentation fault.
__strlen_sse2_pminub () at ../sysdeps/x86_64/multiarch/strlen-sse2-pminub.S:38
38 ../sysdeps/x86_64/multiarch/strlen-sse2-pminub.S: No such file or directory.
I am using Ubuntu 13.10 64-bit. Could somebody please explain, what caused the problem?
Note: I am using scanf instead of cin, because I have specific instruction (from school) to use scanf, printf instead of cin, cout. I wouldn't use it otherwise.
scanfexpects achar *when you use "%s". That said, use astd::stringandoperator>>orstd::getline.std::stringusingscanf, you are attempting to read achar *, which is a c-style string.