Unhandled exception

Oct 16, 2012 at 9:39am
Hi there. I am currently trying to set up a coordinate system for a program I am in the process of creating. I originally set it up in the following way;

double **coords = new double*[n_atoms];

Which worked fine. However, I am currently wanting to change this to the following;

1
2
3
4
5
double **coords = new double*[n_atoms_arb];
	for (int i = 0; i < n_atoms_arb-1; i++)
	{
		coords[i][0]=coords[i][1]=coords[i][2]=0;
	}


where n_atoms_arb is about 50x larger than n_atoms.

However, when I try running this I get an unhandled exception (which I understand very little of) "Unhandled exception at 0x011c2556". Any ideas why this is?

Cheers
Oct 16, 2012 at 9:46am
You have created an array of pointers but you have not made the pointers point anywhere.
1
2
3
4
for (int i = 0; i < n_atoms_arb; i++)
{
	coords[i] = new double[N];
}
Last edited on Oct 16, 2012 at 9:48am
Oct 16, 2012 at 9:59am
Hi Peter, thanks for the reply. Just so I can get my head around what you're saying. Originally I had not made the pointers point anywhere, so when the compiler looked for the location of the point it found nothing and caused an unhandled exception?

So something like this works instead;

1
2
3
4
5
6
const int N = 0;
double **coords = new double*[n_atoms_arb];
	for (int i = 0; i < n_atoms_arb; i++)
{
	coords[i] = new double[N];
}


Achieves what I was wanting?

Cheers
Oct 16, 2012 at 10:28am
Yes. You will have to change N to whatever size it is you want (looks like 3 in your first post). This code will not actually initialize the values in the array. If you want all the number to be zero you can add a couple of parenthesis.
coords[i] = new double[N]();
Topic archived. No new replies allowed.