468,508 Members | 1,476 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,508 developers. It's quick & easy.

declaring constant array without initializing all the elements

Hi,
I need to create a consant array of larze size but however its
elements can be calculated using an equation, say for example I need
an int arry of 20 elements where each element will be

arr[i] = 2 + (i*i)

But I want arry to be constant. How can I declare such a constant
array without actually defining all the elements?

I could think of one way:
Declare a non const arry

int arr_non_const[20];
for(int i=0; i <20; i++)
{
arr_non_const[i] = 2 + (i*i);
}

Now declare a constant pointer

const int* arr = arr_non_const;

Is it correct? Is there any other way to do so.
Mar 3 '08 #1
2 2744
On Mar 3, 7:10*pm, Pavan <pavan...@gmail.comwrote:
Hi,
* * I need to create a consant array of larze size but however its
elements can be calculated using an equation, say for example I need
an int arry of 20 elements where each element will be

arr[i] = 2 + (i*i)

But I want arry to be constant. How can I declare such a constant
array without actually defining all the elements?

I could think of one way:
Declare a non const arry

int arr_non_const[20];
for(int i=0; i <20; i++)
{
* *arr_non_const[i] = 2 + (i*i);

}

Now declare a constant pointer

const int* arr = arr_non_const;

Is it correct? Is there any other way to do so.
Yes, you can do it that way and expose just 'arr' to the external code
that is supposed to use that array.

You can make the arr_non_const as a global pointer initialized by a
initializer function call which holds the actual array (either static
storage duration or dynamically allocated to increase lifetime) but in
an unnamed namespace and fill it up with whatever logic you have
(initializer function). Then declare the pointer 'arr' as you have
above as a global one as well but you declare it as extern (as in C++
const variables have internal linkage), so as to be accessible in
other places.
Mar 3 '08 #2
On Mar 3, 4:30 pm, Lionel B <m...@privacy.netwrote:

[...]
Now one thing you can do is
const int const_arr = [20] = { 2, 3, 6, 11, 18, ... } // you have to
explicitly initialize the array with the 20 elements
As I understood it, the OP explicitly required that the array
elements "... be calculated using an equation", so I suspect
that this would not work for them.
If the equation is known at compile time, it's pretty easy to
write a program which will generate the definition of the array,
with all of its initializers. I do this a lot (although I do
remember having problems with it in one case: the array
contained a couple of million members, and the compiler wouldn't
handle it.)
One technique which might be made to work if the
initialisation function were simple/suitable, would be some
template meta-programming trick, where you get the compiler to
perform the calculation, the canonical example being a
compile-time calculated factorial:
http://en.wikibooks.org/wiki/C%2B%2B...Template_Meta-
Programming#Example:_Compile-time_Factorial
This would be total overkill, though, I suspect.
It's a nice trick for obfuscation, but generating the code with
a separate program is a lot easier and more readable.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 4 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by richbl | last post: by
13 posts views Thread by simondex | last post: by
7 posts views Thread by Chris Wertman | last post: by
6 posts views Thread by Jai Prabhu | last post: by
8 posts views Thread by bintom | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.