Access Violation issue

Hi,

I am a java programmer and I'm very new too c++ and I am having a problem with a bit of code, I am getting an Access violation writing location 0x00000000.

I have this function:

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
double calculateCoefficient(double paper, double lowMid, double highMid, double solid, double referenceLowMid, double referenceHighMid, double coefficientArray[])
{
    double k1;
	double k2;

	paper = round(paper, 3);
	lowMid = round(lowMid, 3);
	highMid = round(highMid, 3);
	solid = round(solid, 3);

	referenceLowMid = round(referenceLowMid, 3);
	referenceHighMid = round(referenceHighMid, 3);

	while(true)
	{
		k1 = getConstant1(paper, lowMid, highMid, solid, referenceLowMid, referenceHighMid);
		k2 = getConstant2(k1, paper, lowMid, highMid, solid, referenceLowMid, referenceHighMid);

		coefficientArray[0] = -(k1 - 2 * k2) / 100;
		coefficientArray[1] = k1 - k2 + (solid - paper) / 100;

		coefficientArray[2] = paper;

		coefficientArray[3] = -(k1 + 2 * k2) / 100;
		coefficientArray[4] = k1 + 3 * k2 + (solid - paper) / 100;
		coefficientArray[5] = paper - 100 * k2;

		double testReferenceHigh = round(calculateValueFromCoefficient(coefficientArray[0], coefficientArray[1], coefficientArray[2], coefficientArray[3], coefficientArray[4], coefficientArray[5], referenceHighMid), 3);

		if(testReferenceHigh < highMid)
		{
			referenceHighMid += 0.001;
		}
		else
		{
			break;
		}
	}

	//Passes back to the coefficientArray.
	return 0;
}


And I am calling it like:

1
2
3
double cyanSampleLCoefficient[6];
	calculateCoefficient(cyanColourValue.paperValueLabL, cyanColourValue.lowMidValueLabL, cyanColourValue.highMidValueLabL, cyanColourValue.solidValueLabL, DEFAULT_LOW_MID, DEFAULT_HIGH_MID, cyanSampleLCoefficient);
	


I am getting an Access violation, but I am not sure why, I think it maybe memory management on my part.

Can anybody help?

Regards


Richard
I am getting an Access violation writing location 0x00000000.


Access violation generally means you're trying to read/write memory that isn't yours. That number is the numerical memory addres you're trying to read/write. THe value 0x00000000 indicates that you're trying to access the null address i.e. you're trying to dereference a null pointer.

Your debugger should identify the exact line causing the issue. You can then interrogate the value of variables at that point and see which one has not been set properly.
A suggestion is since you're passing a lot of class values as parameters, why not just pass the entire object? Use the function to seperated what you need. This would save you a lot of effort when calling your function since you don't have to remember each parameter and limits the amount of overall typing.

Edit: you are also calling a function that returns type double but aren't returning the value to any functions or variables or conditions.
Last edited on
Thanks for your help guys.

The reason I do not send in the object is because there are values for:

cyanColourValue.paperValueLabL
cyanColourValue.paperValueLabA
cyanColourValue.paperValueLabB
cyanColourValue.paperValueLabC
cyanColourValue.paperValueLabM
cyanColourValue.paperValueLabY
cyanColourValue.paperValueLabK

just for the first parameter and this is the best way I could find.

After a little debugging I found it was my round command, that rounds to 3 decimal places giving the Access Violation error, I do not know why as I copied this from another forum. :-S

here it is anyway:

1
2
3
4
5
6
7
8
9
10
11
12
13
double round(double value, double places){
	double off = pow(10, places);
	return cint(value * off) / off;
}

double cint(double x){
	if (modf(x, 0) >= 0.5){
		return x >= 0 ? ceil(x):floor(x);
	}
	else{
		return x < 0 ? ceil(x):floor(x);
	}
}


Thanks again for your help.


Regards


Richard
Topic archived. No new replies allowed.