segmentation fault in linux
Feb 13, 2012 at 9:14am UTC
Hello,
I wrote a program to evaluate some files. Compiled and run in windows-cmd it worked perfectly, but now I am trying to use it in Ubuntu 10.04 and am getting the "Segmentation fault". It seems that the program can not read the files.
here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <math.h>
int main();
std::vector<double > readVector(std::string n);
using namespace std;
int main()
{
//Pfad angeben
string PFAD = "/home/maxeinerl/FOAM_RUN/beispielfiles" ;
//einlesen
vector<double > retvol = readVector(PFAD + "/Retentionsvolumen" );
vector<double > inletpeak = readVector(PFAD + "/InletPeak" );
vector<double > outletpeak = readVector(PFAD + "/OutletPeak" );
//Normieren
double summeinlet = 0;
double summeoutlet = 0;
for (unsigned int i = 0; i < inletpeak.size(); i++)
{
summeinlet = inletpeak[i] + summeinlet;
summeoutlet = outletpeak[i] + summeoutlet;
}
double deltainout = ( summeinlet - summeoutlet ) / summeinlet;
cout << "Summeinlet: " << summeinlet << "; Summeoutlet: " << summeoutlet
<< "; Normiertes Delta In-Out: " << deltainout << endl;
vector<double > norminlet = inletpeak;
vector<double > normoutlet = outletpeak;
for (unsigned int i = 0; i < inletpeak.size(); i++)
{
norminlet[i] = inletpeak[i] / summeinlet;
normoutlet[i] = outletpeak[i] / summeoutlet;
//cout << norminlet[i] << ", " << normoutlet[i] <<endl;
}
//müh t berechnen über retvol
double muehtinletzaehler = 0;
double muehtinletnenner = 0;
double muehtoutletzaehler = 0;
double muehtoutletnenner = 0;
double muehtinlet = 0;
double muehtoutlet = 0;
for (unsigned int i = 0; i < norminlet.size() -1 ; i++)
{
muehtinletzaehler = retvol[i] * norminlet[i] * (retvol[i+1] - retvol[i]) + muehtinletzaehler;
muehtinletnenner = norminlet[i] * (retvol[i+1] - retvol[i]) + muehtinletnenner;
muehtoutletzaehler= retvol[i] * normoutlet[i] * (retvol[i+1] - retvol[i]) + muehtoutletzaehler;
muehtoutletnenner = normoutlet[i] * (retvol[i+1] - retvol[i]) + muehtoutletnenner;
}
muehtinlet = muehtinletzaehler / muehtinletnenner;
muehtoutlet = muehtoutletzaehler / muehtoutletnenner;
cout << "Muehtinlet: " << muehtinlet << " Muehtoutlet: " << muehtoutlet << endl;
//digmatquadrat betrechnen über retvol und mueht
double sigma2tinletzaehler = 0;
double sigma2tinletnenner = 0;
double sigma2toutletzaehler = 0;
double sigma2toutletnenner = 0;
double sigma2tinlet = 0;
double sigma2toutlet = 0;
for (unsigned int i = 0; i < norminlet.size() -1 ; i++)
{
sigma2tinletzaehler = (retvol[i] - muehtinlet) * (retvol[i] - muehtinlet) * norminlet[i] * (retvol[i+1] - retvol[i]) + sigma2tinletzaehler;
sigma2tinletnenner = norminlet[i] * (retvol[i+1] - retvol[i]) + sigma2tinletnenner;
sigma2toutletzaehler= (retvol[i] - muehtoutlet) * (retvol[i] - muehtoutlet) * normoutlet[i] * (retvol[i+1] - retvol[i]) + sigma2toutletzaehler;
sigma2toutletnenner = normoutlet[i] * (retvol[i+1] - retvol[i]) + sigma2toutletnenner;
}
sigma2tinlet = sigma2tinletzaehler / sigma2tinletnenner;
sigma2toutlet = sigma2toutletzaehler / sigma2toutletnenner;
cout << "sigma2tinlet: " << sigma2tinlet << " sigma2toutlet: " << sigma2toutlet << endl;
//masima bestimmen
unsigned int inletmax = 0;
unsigned int outletmax = 0;
for (unsigned int i = 0; i < norminlet.size() -1 ; i++)
{
if (norminlet[i+1] >= norminlet[i] && norminlet[i+1] >= norminlet[inletmax])
inletmax=i+1;
if (normoutlet[i] <= normoutlet[i+1] && normoutlet[i+1] >= normoutlet[outletmax])
outletmax=i+1;
}
cout << "Inletmaximum bei: " << retvol[inletmax] << ", Outletmaximum bei: " << retvol[outletmax] << endl;
//Plate number n = ( t2 / sigma2 ) über retvol
double platenumberinlet = (retvol[inletmax] * retvol[inletmax]) / sigma2tinlet;
double platenumberoutlet = (retvol[outletmax] * retvol[outletmax]) / sigma2toutlet;
cout << "Platenumberinlet: " << platenumberinlet << ", Platenumberoutlet: " << platenumberoutlet << endl;
return 0;
}
//vektoreinlesen funktion
vector<double > readVector(string n){
string row;
vector<double > v;
vector<string> rows;
bool read;
ifstream file;
file.open(n.c_str());
while (getline(file,row)) rows.push_back(row);
for (unsigned int i = 0; i < rows.size(); ++i)
{
if (rows[i] == "(" )
read = true ;
if (rows[i] == ")" )
read = false ;
if (read)
v.push_back(atof(rows[i].data()));
}
file.close();
return v;
}
It seems to me that the program is not able to read the files, because deltatinout displays -nan which means that there are only zeros read.
Thanks for your help...
Feb 13, 2012 at 11:56am UTC
Yes, if the files cannot be opened the program crashes on line 31. Since 'summeinlet' is 0.
You should always check that you don't divide by 0. Which could happen several times in your program.
Further more you expect on line 29 that 'outletpeak' has the same size like 'inletpeak' which is crash prone too.
Feb 13, 2012 at 2:38pm UTC
i know that it could occur that there is divide by 0, but this only happens if the program cannot read the files...
so the question is why is the programm not able to open the files?
Feb 13, 2012 at 2:45pm UTC
Linux is case sensitive. Also make sure they have read and write permissions for your user.
Topic archived. No new replies allowed.