simple progam --- why is there a segmentation fault?

I have a simple program that reads in this file and bins the data to make a histogram. The data are written to an output file. I get an unknown segmentation fault, but I have looked at this very carefully and cannot figure out there this is coming from. I have tried using gdb without luck. My main() calls a function called corrFinder which does the binning with the use of two for-loops. Both loops seem to run over the right number of times, as can been seen from the cout comments I have put after each step.

Here is a very short version of the file it reads in:
290851 1 0.0809365 -1.30974 1.90152 0 1 1 -6.18079
131813 1 -2.0661 0.862301 1.00005 0 0 1 -6.93275
45993 2 -2.34283 -2.02655 1.39487 0 1 1 -3.17302
237383 1 2.98952 -0.703702 0.460904 0 1 0 -6.72949
77093 1 2.65232 -0.633404 3.73957 1 0 0 -6.66136
52659 1 5.51981 -1.4715 2.22086 1 1 0 -6.5316
113737 2 5.81654 0.156132 0.223804 0 1 1 -3.16736
239218 1 8.61948 -1.22876 0.722355 0 0 3 -6.79347
113288 1 7.49012 1.08393 2.43977 0 1 1 -6.63733
14559 2 10.1298 -0.867803 3.10744 1 0 2 -3.21186

I think the seg fault is happening somehow in one of the for-loops inside the function corrFinder(), because the comment just after the loops is not reached. However the i and j from both loops go until they are suppose to stop. This is why I can't figure out why there is a problem.

Thanks for any guidance!

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
#include <iostream>
#include <fstream>
#include <iomanip> //allow setprecision to get all the decimal points
#include <vector>
#include <string>
#include <cmath>
using namespace std;

int corrFinder(vector<vector<double> > & g,  int count)// correlation function
{
    cout << "Entering function" << endl;
    const double r_ave=76.41; //average pair seperation
    const double Lx=401.06;
    const double Ly=401.06;
    const double Lz=32.87;
    const double cry_pe=6.75956;
    const double b=40; //bin width factor
    double x=0,  y=0,  z=0;
    float n=0;
    vector<double> N(1,0); //number of pairs in certain separation range
    vector<double> CE(1,0); //running sum of E_i*E_j
    cout << "entering function loop" <<endl;
    	for (int i=0; i<count-1; i++)
        {
	    cout << "i= " << i <<endl;
	    for (int j=i+1; j<count; j++)
	    {   cout << "count= "<<count<<endl;
	        cout << "j= " << j <<"\t"<<"i= "<<i<<endl;
		if (g[i][2]-g[j][2] > Lx/2) /**check x coor.**/
                {
		    x=g[i][2]-g[j][2]-Lx;
		    cout << "if 1" << endl;
		}
		else if (g[i][2]-g[j][2] < -Lx/2)
		{
		    cout << "if 2" << endl;
		    x=g[i][2]-g[j][2] + Lx;
		}
		else
		{
		    cout << "if 3" << endl;
		    x=g[i][2]-g[j][2];
		}
		if (g[i][3]-g[j][3] > Ly/2) /**check y coord.**/
                {
		    cout << "if 4" << endl;
		    y=g[i][3]-g[j][3]-Ly;
		}
		else if (g[i][3]-g[j][3] < -Ly/2)
		{
		    cout << "if 5" << endl;
		    y=g[i][3]-g[j][3] + Ly;
		}
		else 
		{
		    cout << "if 6" << endl;
		    y=g[i][3]-g[j][3];
		}
		if (g[i][4]-g[j][4] > Lz/2)/**check z coord.**/
                {
		    cout << "if 7" << endl;
		    z=g[i][4]-g[j][4]-Lz;
		}
		else if (g[i][4]-g[j][4] < -Lz/2)
		{
		    cout << "if 8" << endl;
		    z=g[i][4]-g[j][4] + Lz;
		}
		else
		{
		    cout << "if 9" << endl;
		    z=g[i][4]-g[j][4];
		}
		n=floor(sqrt(x*x+y*y+z*z)/(r_ave/b));
		cout << "after n" << endl;
		N[n]=N[n]+1;
                cout << "after N" << endl;
		CE[n]=CE[n]+(g[i][8]+cry_pe)*(g[j][8]+cry_pe);
		cout <<"after CE"<<endl;
	    }
	}
    ofstream myfile;
    myfile.open ("testOutput");
    for(int i=0; i<N.size(); i++)
    {
      myfile << CE[i]/(N[i]-1) << endl;
    }
    myfile.close();
    return 0;
}
//Main()_______________________________________________________________________
int main() 
{
  int numRows=10;
  int numCols=9;
  int count=0;
  ifstream myfile("/home/adam/research_nanocomposite/amorphous_restart/testdump");
  vector<vector<double> > f; //initialize with no size!
  for (int i=0; i<numRows; i++)
  {
    vector<double> row; // Create an empty row
    for (int j=0; j<numCols; j++) //for the row
    {
	double temp;
	myfile >> temp;
	row.push_back(temp); // Add an element to the row
    }
	if(row.at(1)==1)
	{	
	  count = count+1;
	  f.push_back(row); // Add the whole row vec to the main vector
	}
  }
  for(int i=0; i<count; i++)
  {
     for (int j=0; j<numCols; j++)
     {
	cout << f[i][j] << "\t"; //check our array
     }
     cout << endl;
  }
  cout << "size of f is " << f.size()<< endl;
  corrFinder(f, count);
  return 0;
}
Last edited on
$ valgrind ./a.out
==3092== Memcheck, a memory error detector
==3092== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3092== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==3092== Command: ./a.out
==3092== 
290851	1	0.0809365	-1.30974	1.90152	0	1	1	-6.18079	
131813	1	-2.0661	0.862301	1.00005	0	0	1	-6.93275	
237383	1	2.98952	-0.703702	0.460904	0	1	0	-6.72949	
77093	1	2.65232	-0.633404	3.73957	1	0	0	-6.66136	
52659	1	5.51981	-1.4715	2.22086	1	1	0	-6.5316	
239218	1	8.61948	-1.22876	0.722355	0	0	3	-6.79347	
113288	1	7.49012	1.08393	2.43977	0	1	1	-6.63733	
size of f is 7
Entering function
entering function loop
i= 0
count= 7
j= 1	i= 0
if 3
if 6
if 9
==3092== Invalid read of size 8
==3092==    at 0x401C4E: corrFinder(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&, int) (foo.cpp:76)
==3092==    by 0x40212C: main (foo.cpp:121)


you are reading (and later writing) out of bounds
1
2
3
//vector<double> N(1,0); //number of pairs in certain separation range
		n=floor(sqrt(x*x+y*y+z*z)/(r_ave/b));
		N[n]=N[n]+1;
`N' has size 1 (¿why is it a vector instead of an scalar?) so the only valid position is `N[0]'.
However, inspecting the value of `n', it has 1.
Hi, Thanks for the reply. Yes, I initialized N to be of size 1 with value of 0. However, the <vector> container class should dynamically allocate whatever size it needs as N grows with the loop, right?
Vectors only grow when you either manually resize it, or when you use methods like push_back or insert. Using [] does NOT make it grow; you will just get segfaults like you are experiencing.
Thanks!
Topic archived. No new replies allowed.