Frank Frank - 2 months ago 19
C++ Question

c++ strange Segmentation fault appears

Description



SFDD is a class that I created in SFDD/src/, following is file
test0.cpp
, which is used to test this class SFDD.

#include <iostream>
#include <string>
#include "SFDD.h"

int main(int argc, char** argv) {

vector<int> vars_order;
int var_no = 18;
for (int i = 1; i <= var_no; ++i) vars_order.push_back(i);

Vtree* v = new Vtree(1, var_no*2-1, vars_order);
v->save_file_as_dot("vtree");

Manager m(v);

SFDD sfdd1 = m.sfddZero(); // create a Zero SFDD

SFDD sfdd2 = m.sfddOne();

SFDD sfdd3 = m.sfddVar(3);

SFDD sfdd4 = m.sfddVar(11);

SFDD sfdd6 = sfdd3.And(sfdd4, m, true);
sfdd6.save_file_as_dot("f=x11_and_x3"); // export sfdd6

cout << "Haha 1" << endl; // flag 1: for debugging

SFDD sfdd8 = sfdd4.Xor(sfdd6, m, true);
sfdd8.save_file_as_dot("f=x11_xor_(x11_and_x3)");

cout << "Haha 2" << endl; // flag 2: for debugging
sfdd3.Xor(sfdd8, m, true).save_file_as_dot("f=x3_xor_x11_xor_(x11_and_x3)");

cout << "Haha 3" << endl; // flag 3: for debugging

return 0;
}


After
make
and run
./test0
.
I got

fang@ubuntu:~/Desktop/Link to GitHub/SFDD$ ./test0
Haha 1
Haha 2
Haha 3


which means it works.

But!!!

1. Segmentation fault 1



After I comment this line

sfdd6.save_file_as_dot("f=x11_and_x3"); // export sfdd6


I
make
and run
./test0
again , I got

fang@ubuntu:~/Desktop/Link to GitHub/SFDD$ ./test0
Haha 1
Segmentation fault (core dumped)


This comfuses me, because I design function
save_file_as_dot(string s)
just to export class SFDD to
dot
file, it shouldn't have ability to avoid a segmentation fault.

2. Segmentation fault 2



After I comment this line (uncomment above line this time)

SFDD sfdd1 = m.sfddZero(); // create a Zero SFDD


I got

fang@ubuntu:~/Desktop/Link to GitHub/SFDD$ ./test0
Haha 1
Haha 2
Segmentation fault (core dumped)


This comfuses me too, because last several lines don't use object
sfdd1
, how can it avoid sementation fault?

I paste the part of
save_file_as_dot(string s)
in SFDD/src/SFDD.cpp here



void SFDD::print_dot(fstream & out_dot, bool root, int depth, string dec_name) const {
if (root) {
out_dot << "digraph G {" << endl;
if (is_terminal()) {
if (is_constant()) {
out_dot << "\t" << value << " [shape=record, label=\"" \
<< value << "\"]" << endl << "}";
// << value << " ~ " << vtree_index << "\"]" << endl << "}";
} else {
out_dot << "\t";
if (is_negative()) out_dot << "neg";
out_dot << "x" << value/2 << " [shape=record, label=\"";
if (is_negative()) out_dot << "-";
out_dot << "x" << value/2 << "\"]" << endl << "}";
// out_dot << "x" << value/2 << " ~ " << vtree_index << "\"]" << endl << "}";
}
return;
}
}
if (elements.empty()) {
if (value < 2) {
out_dot << value;
// out_dot << value << "~" << vtree_index;
} else {
if (is_negative()) out_dot << "-";
out_dot << "x" << value/2;
// out_dot << "x" << value/2 << "~" << vtree_index;
}
return;
}
++depth;
out_dot << "\t" << dec_name << " [shape=circle, label=\"" << vtree_index << "\"]" << endl;
string e_name = "Ele_" + to_string(depth) + "_1";
for (vector<Element>::const_iterator e = elements.begin();
e != elements.end(); ++e) {
e_name = check_dec_name(e_name);
out_dot << "\t" << dec_name << " -> " << e_name << endl;
e->print_dot(out_dot, depth, e_name);
}
if (root) out_dot << "}" << endl;
}

void SFDD::save_file_as_dot(const string f_name) const {
fstream f;
f.open("dotG/test0/"+f_name+".dot", fstream::out | fstream::trunc);
print_dot(f, true);
f.close();
}


Reference



full code: https://github.com/fangbq/SFDD/blob/master/src/SFDD.cpp

Question



Why these segmentation faults appear?
How to fix it?

Maybe you have some ideas.

Answer Source

You should be aware of the concept of Undefined Behavior, in your circumstances it's pretty safe to assume that your code invokes such behavior at some point and it sometimes causes the program to abruptly terminate due to a segmentation fault and sometimes it just doesn't.

To solve this problem, you need a tool like valgrind. Using this tool or it's equivalent for your OS and environment, you should be able to find the exact place where the violation occurs and solve the apparently random segmentation fault that appears and disappears depending on parts of the code that do not have any relation to the real problem.