Hi,
I am getting "Segmentation fault" error in line number in the following function.
deque <string> text;
deque <string> text_1;
int circuit::writetofile(int maxno,char *fname)
{
/* Before writing the clauses correct the line p cnf */
//deque<string> text;
std::string s,t;
/* Counting the number of original clauses in the file */
ifstream file_cnf;
int size_c = 0;
/* Count the number of lines in the file */
string linec;
linec.clear();
s.clear();
t.clear();
file_cnf.open(fname,ios::in);
if (!file_cnf.is_open())
{
cout<< "Error opening file" << "\n";
exit(-1);
}
else
{
fprintf(stderr,"I am here C \n");
while(getline(file_cnf,linec))
{
size_c++;
}
fprintf(stderr,"I am here D \n");
file_cnf.clear();
file_cnf.close();
cout <<"----------------Writetofile()--------------- \n" << endl;
printf("File contains lines = %d \n",size_c);
}
int temp2 = (size_c-11)+columns.size();
std::string t1;
std::string t2;
std::string t3 = "p cnf";
std::string t4 = " ";
std::stringstream outstring1;
std::stringstream outstring2;
outstring1 << maxno;
outstring2 << temp2;
t1 = outstring1.str();
t2 = outstring2.str();
cout << "\n" << "Number of lines after adding clauses : " << temp2 + 11 << "\n" << endl;
t = t3 + t4 + t1 + t4 + t2;
/* Changing the line */
// load the file
ifstream inf(fname); // You must supply the proper filename
if (!inf) // Check for errors here
{
// Print an error and exit
cerr << "The sequential file s298.cnf cannot be opened for reading!" << endl;
exit(1);
}
fprintf(stderr,"I am here A \n");
cout << "text.size() = " <<text.size() << "\n";
while (getline( inf, s ))
text.push_back( s );
fprintf(stderr,"I am here B \n");
if (!inf.eof()) // and here
{
// Print an error and exit
cerr << "No end of file!" << endl;
exit(1);
}
inf.close();
//erase the text
text.erase(text.begin() + 10);
// modify the text
text.insert(text.begin() + 10, // again, you must supply the line number
string(t)
);
// rewrite the new text to file
ofstream outf(fname);
if (!outf)
{
// Print an error and exit
cerr << "The sequential file s298.cnf cannot be opened for reading!" << endl;
exit(1);
}
for (deque <string> ::iterator
line = text.begin();
line != text.end();
++line)
outf << *line << '\n';
outf.close();
// clearing deque
while(!text.empty())
{
text.pop_back();
}
// deallocating memory
deque<string>(). swap(text);
}
This function is called from some other function many times. The segmentation fault occurs at line number "while (getline( inf, s ))" marked in green, when this function is called ith time where i is around 1000.
I think there is some issue in deque. Am i deallocating the memory used by deque correctly? Should i clear the string? Should i use vector?
When I debug the function and do a backtrace i get the following error:
#0 0xb759cb36 in std::__default_alloc_template<true, 0>::allocate(unsigned) () from /usr/lib/libstdc++.so.5
#1 0xb75a2b68 in std::string::_Rep::_S_create(unsigned, std::allocator<char> const&) () from /usr/lib/libstdc++.so.5
#2 0xb75a2c99 in std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned) () from /usr/lib/libstdc++.so.5
#3 0xb75a0a06 in std::string::reserve(unsigned) () from /usr/lib/libstdc++.so.5
#4 0xb75a0fac in std::string::append(unsigned, char) () from /usr/lib/libstdc++.so.5
#5 0xb7592a42 in std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char) () from /usr/lib/libstdc++.so.5
#6 0xb7592ab8 in std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) () from /usr/lib/libstdc++.so.5
#7 0x080549a4 in circuit::writetofile(int, char*) (this=0x80827e0, maxno=426,
I cannot understand anything from it. Can someone please help me with it. I am stuck...
Thanks