getting bus error

Hi,

I have a code which is compiled but it's giving bus error during execution, I could not understand the problem. Any help regarding this matter would be appreciated. Thanks. The code is like the following

#include <iostream>

using std::cout;
using std::cin;
using std::ios;
using std::cerr;
using std::endl;
using std::left;
using std::right;


#include <iomanip>

using std::setw;
using std::setprecision;

#include <fstream>

using std::ifstream;
using std::ofstream;

#include <cmath>
#include <cstdlib>

int main(int argc, char* argv[])
{
if (argc != 4) {
cout << "Argument List: xbox ybox zbox" << endl;
exit(1);
}

float xbox = atof(argv[1]);
float ybox = atof(argv[2]);
float zbox = atof(argv[3]);

// assign absolute path to variable "path"
char *path=NULL;
size_t size;
path=getcwd(path,size);

ifstream inputFile( "crystal.xyz", ios::in );
if ( !inputFile )
{
cerr << "File could not be opened" << endl;
exit(1);
}

int n,i,counter,j,k;
double x;
double y;
double z;
char tmp[200],atype[3];
float * xx, * yy, * zz;
float dx,dy,dz,rij;
int n_particles = 2000;
double r_cut=1.375;

int coordination[n_particles];
for(i=0; i< n_particles; i++)
coordination[i] = 0.0;

while (1) {

n=0;
if (inputFile.eof() == 1){
//std::cout << "end of file, breaking from loop\n";
break; //test whether we have reached the end
}
inputFile >> n;
inputFile.getline(tmp,200);
counter++;
if (counter==1) { //assumes the particle number is not changing
xx = new float[n]; //now we know the size
yy = new float[n];
zz = new float[n];

}
for (i=0;i<n;i++) { //loop over n atoms
inputFile >> atype >> xx[i] >> yy[i] >> zz[i];
}
for (i=0;i<(n-1);i++) {
for (j=i+1;j<n;j++) {
dx = xx[i] - xx[j];
dy = yy[i] - yy[j];
dz = zz[i] - zz[j];

// invoke minimum image convention
if (dx > xbox/2.0) dx = dx - xbox;
if (dx < -1.0*xbox/2.0) dx = dx + xbox;
if (dy > ybox/2.0) dy = dy - ybox;
if (dy < -1.0*ybox/2.0) dy = dy + ybox;
if (dz > zbox/2.0) dz = dz - zbox;
if (dz < -1.0*zbox/2.0) dz = dz + zbox;

rij = sqrt(dx*dx + dy*dy + dz*dz);
if (rij < r_cut)
coordination[i]++;

}

}
}
for (k=0; k < n_particles; k++) {
cout << k << "\t" << coordination[k] << endl;
}

inputFile.close();
delete [] xx;
delete [] yy;
delete [] zz;

return 0;
}


Last edited on
The code don't even compile. You missed a semicolon after inputFile.close()
sorry, I missed it during the time of pasting here.
You never initialize counter
Thank you for your help
Topic archived. No new replies allowed.