Unhandled exception

Hi there,

I have a function that reads data from a text file, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void coords(int &atom_number, double *&x_point, double *&y_point, double *&z_point, int *&type_point,)
{

                        Dfile >> atom_number; // This has a value of 5

			for (int i=0; i<atom_number; i++)
			{
                        Dfile >> type_point[i];
			Dfile >> x_point[i];
			Dfile >> y_point[i];
			Dfile >> z_point[i];
			}

			Dfile.close();
}


Which is then used in the main file like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
double **coords_fluid = new double*[5000];
for (int i = 0; i < n_atoms_methane; i++)
{
	coords_fluid[i] = new double[4];
}

int k=0;

for( int i = 0; i < n_atoms_methane; i++ )
{
	for( int j = 0; j < 4; j++ )
	{

		k=k+1;	
	        coords_fluid[k][0] = rand(0,1)+x_point[j]*15.0; 
		coords_fluid[k][1] = rand(0,1)+y_point[j]*15.0;
		coords_fluid[k][2] = rand(0,1)+z_point[j]*15.0;
		coords_fluid[k][3] = type_point[j];
	}
}


The purpose of this loop is to create a coordinate system consisting of randomly generated coordinates inside these (x,y,z) points that have been input from the text file.

Now, the problem is that when I run the program I get an unhandled exception during the last loop I've shown here. I suspect it is something to do with array sizes but I'm a bit confused by this.

Does anyone notice something I've not?

Cheers





That's a very odd way to increment k. What is the value of n_atoms_methane? Is it 5000? If not, where did that 5000 come from?
My guess is that you're trying to write past the end of the coords_fluid array. You're not given us enough information to know for sure, as we don't know what the value of n_atoms_methane is.

Your best bet is to use a debugger, or add some debugging output. That will give you an idea of the state of the loop when it crashes. In particular, you'll want to look at which element of coords_fluid is being written to when it crashes.
apparently, x_point, y_point, z_point and type_point all have a size of 5 (assuming that they are the arrays referenced in void coords. If so, why does the loop over j only iterate from 0-3?
Would you not want k=k+1; after the rand() stuff?
You look like you are indexing coords_fluid[][] from [1][0] not [0][0].
Topic archived. No new replies allowed.