Since it's const I presume you only really want to initialize it once and then use it read only? If so, initialize it when you define it. Simplified example below.
Main Topics
Browse All TopicsI 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.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
thanks inifinity & evilrix for responses.
@infinity,
the compiler did not give me an error or even a warning when I compiled it. but as you said the compiler can place it wherever it deems appropriate and that's what I too thought was happening. so it is definitely not a reliable way to modify const data. also as evilrix said, I want to initialize it once and then use it as read-only. so making it non-const it not an option.
@evilrix,
I will have to go with your second comment because I don't trust the compiler. They way you suggested to initialize may cause me some problems. I did not include entire initialize function for the sake of brevity but further down I use statements that use values from previous array elements (see the attached code). I don't know which element will be constructed first (0th or Nth). If it is the reverse order all my values will be messed up. (correct me if C++ spec says otherwise)
My other option is to hard code numbers as in your first comment but it is too error prone and unmaintainable.
I modified it a little so DefinedTypes is now declared as
public: static const Dimensions * const DefinedTypes;
and initialized as
const Dimensions * const DimQty::DefinedTypes = initialize();
Thanks again for you input.
Business Accounts
Answer for Membership
by: Infinity08Posted on 2008-06-29 at 13:18:49ID: 21895324
const means that you cannot modify the contents of the array ... You are trying to do exactly that in your initialize function.
It's true that that won't necessarily cause an access violation, but the possibility exists (depending on where the compiler decided to place the data), so you shouldn't try to modify const data. In fact, your compiler probably threw an error when you tried to.
Why not just remove the const (since you obviously don't want it to be const) ?