Advertisement

06.29.2008 at 01:10PM PDT, ID: 23525313
[x]
Attachment Details

Access violation while writing to const array

Asked by jhshukla in C++ Programming Language, Windows XP Operating System, .NET

Tags: C++, C#, .net, dll, const, access violation, pointer

I am working on a library to deal with physical quantities like Displacement, Velocity, etc. This project produces a .LIB file.
There is a native DLL project that uses this .LIB; and then there is a .Net exe that dynamically loads this DLL.

There is a const array static member in class DimQty. it contains combinations of dimensions for known types (speed = length/time, density = mass/length^3, etc). the array is not initialized to desired values for two reasons:
(1) it would require **many** inline function calls in the initializer list which is plain ugly. e.g. array = { func1(), func2()/func829() };
(2) it would require reordering enums based on dependency causing readability problems;
Since the array is not initialized to desired values, I have a function, aptly named initialize(), that will take care of filling appropriate values. I get access violation in initialize(), when I try to write to the array.

The code compiles fine without warnings around the troublesome location. there are other minor warnings like comparing int and bool etc. which I ignored.

I have two possible ideas as to why this is happening (there may be other reasons that I don't know of):
(1) since the array is a class static const variable it is not on the stack (stack segment), but somewhere in a section marked "read-only." this would cause the hardware to signal an interrupt.
(2) .Net environment is messing up something. unlikely.

I need to know why this is happening and how to fix it. If my reasoning is correct, then the only way to fix it may be to change from static array to dynamically allocated array.

Help appreciated. Thanks.Start Free Trial
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:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
/*=========== class/variable definitions in .LIB: =============*/
namespace Physics {
	struct Dimensions {
		float Current, Length, LuminousIntensity,
			Mass, Quantity, Temperature, Time;
	};
 
	class DimQty {			// abstract class
	public:
		enum Type {
			Null,			// dimensionless quantity
			/* SI base types */
			Current,	...
			/* SI derived types */
			...
			/* ------------------------ */
			Unknown // wildcard for any combo not listed above
		};
 
		/// this is the array that is mentioned in title
		static const Dimensions DefinedTypes[Unknown]; 
 
		static void initialize();
 
		// ctor, dtor & other funcs go here
	protected:
		static bool inited;
		Dimensions * dims;
	};
}
// ------------- src ------------
const Dimensions DimQty::DefinedTypes[DimQty::Unknown] = {{0}};
bool DimQty::inited = false;
void DimQty::initialize()
{
	Dimensions * def = const_cast<Dimensions*>(DefinedTypes);
	def[Current].Current				= 1;<< access violation here
	def[Length].Length				= 1;<< and here
	def[LuminousIntensity].LuminousIntensity	= 1;<< and here
	def[Mass].Mass					= 1;<< etc
	def[Quantity].Quantity				= 1;<<
	def[Temperature].Temperature			= 1;<<
	def[Time].Time					= 1;<<	
	...
}
 
DimQty::DimQty(const Dimensions *_dims)
{
	if (!inited)
		initialize();
	...
}
 
 
Loading Advertisement...
 
[+][-]06.29.2008 at 01:18PM PDT, ID: 21895324

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06.29.2008 at 02:01PM PDT, ID: 21895458

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.29.2008 at 02:09PM PDT, ID: 21895482

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C++ Programming Language, Windows XP Operating System, .NET
Tags: C++, C#, .net, dll, const, access violation, pointer
Sign Up Now!
Solution Provided By: evilrix
Participating Experts: 2
Solution Grade: A
 
 
[+][-]06.29.2008 at 11:25PM PDT, ID: 21897000

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628