C++: exception thrown during debugging of code on VS2015

Hello fellow C++ coders, I have some code that builds but during debugging with local windows debugger (going down line by line) I get this error that aborts the program:

Exception thrown at 0x01394FDA in MonteCarloDemo.exe: 0xC0000005: Access violation reading location 0xCCD6AACC.
If there is a handler for this exception, the program may be safely continued.

I check the hexadecimal value and I have traced it to this function:

1
2
3
4
5
6
7
8
9
10
11
double Mie::GetCext(double lambda) {
	int l;
	l = int(floor((lambda - LambdaMin) / LambdaDiff));
	if (l<0) {
		l = 0;
	}
	if (l >= LambdaNum) {
		l = LambdaNum - 1;
	}
	return MD[l].Cext;
}


Some explanation of the variables: This function is a member of the class Mie as you can see. It is meant to return an extinction cross section for light. Here are some of the associated other functions in the Mie class and the calls to the functions in int main();

Member functions of Mie Class:

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
void Mie::setLambdaMin(int value1) {

	LambdaMin = value1;

}

void Mie::setLambdaMax(int value2) {

	LambdaMax = value2;

}

double Mie::getLambdaMin(void) {

	return LambdaMin;

}


double Mie::getLambdaMax(void) {

	return LambdaMax;

}
double Mie::getLambdaDiff(void){

	LambdaDiff = (LambdaMax - LambdaMin) / (double)LambdaNum;

	return LambdaDiff;

}


Function calls in main:
1
2
3
4
5
6
7
8
9
10
       Mie m1;
       m1.setLambdaNum(100);
	numofLambda = m1.getLambdaNum();
	m1.setLambdaMin(650.0); //setting the near-IR range
	m1.setLambdaMax(1350.0);
	double maxWaveL = m1.getLambdaMin();
	double minWaveL = m1.getLambdaMax();
	double lambdaDiff = m1.getLambdaDiff();
         double Cext = m1.GetCext(monochr_wavel);


If anyone can please tell me what is throwing this exception I would be extremely grateful as it is just crashing the debuggng.. many thanks.
Last edited on
From the code you provide I can see only one option for a read access error
return MD[l].Cext; l might be out of bounds of MD

To be certain you need to inspect the value of l in the debugger.
@Thomas1965 Thank you Thomas, I will try this and get back to this forum if I am still having problems.
@Thomas1965 or anyone else seeing the question, the value of int l was 20 so it should be MD[20].Cext; for the wavelength range of 650.0 and 1350.0 but how does that help solve the problem? The exception gets thrown at MD[l].Cext when I step into the function not at int l and not at
l = int(floor((lambda - LambdaMin) / LambdaDiff));
Last edited on
how does that help solve the problem

It tells you that an index of 20 may be beyond the end of your MD array. (Or whatever MD is, since you haven't shown us the definition.)
Topic archived. No new replies allowed.