Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Initializing const arrays

Credits go to kazzmir for helping out!
This thread is locked; no one can reply to it. rss feed Print
Initializing const arrays
SiegeLord
Member #7,827
October 2006
avatar

I have a C style const array and I want to initialize at compile time (not like I have any choice in that matter) and I want to do it like this:

const int arr[2];
arr[0] = 1;
arr[1] = 2;

Naturally, that does not compile. Is something like that impossible to do in C++?

Note that I am well aware of how to initialize const arrays using the standard method. This is not what I want:

const int arr[] = {1, 2};

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Vanneto
Member #8,643
May 2007

const int arr[2] = {1, 2};

In capitalist America bank robs you.

X-G
Member #856
December 2000
avatar

Too bad, that's what you're getting.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

SiegeLord
Member #7,827
October 2006
avatar

Very well, do I get any improvements if I drop the const requirement?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

X-G
Member #856
December 2000
avatar

Depends on what you mean by "improvement".

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Wetimer
Member #1,622
November 2001

What's wrong with the standard method? Perhaps if you give more information we can suggest a way to do whatever you are trying to do.

<code>if(Windows.State = Crash) Computer.halt();</code>

kazzmir
Member #1,786
December 2001
avatar

Here is a fun gcc extension

const int x[] = { [0] = 1, [3] = 9 };

Which means exactly what it looks like, initialize element 0 to 1, and element 3 to 9. All other elements are 0.

SiegeLord
Member #7,827
October 2006
avatar

Woah... :o
GCC extensions are rather cool. Nested functions etc... I thought I'd be switching to D but I guess GCC+C99 is where it shall be.

For now I'll end up using this:

1#define ARR(a) const int EN ## a
2 
3ARR(1) = 3;
4ARR(2) = 2;
5 
6#ifndef EN1
7#define EN1 0
8#endif
9 
10#ifndef EN2
11#define EN2 0
12#endif
13 
14#ifndef EN3
15#define EN3 0
16#endif
17 
18#ifndef EN4
19#define EN4 0
20#endif
21 
22const int arr[] = { EN1, EN2, EN3, EN4};

Never said it has to be pretty.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Wetimer
Member #1,622
November 2001

???

How in the world is that better then doing

const int arr = {
  /* 0 */ 0,
  /* 1 */ 3,
  /* 2 */ 2,
  /* 3 */ 0
};

<code>if(Windows.State = Crash) Computer.halt();</code>

ReyBrujo
Moderator
January 2001
avatar

Because you can modify those values at compile time with -DEN1=10

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

SiegeLord
Member #7,827
October 2006
avatar

I was planning on using this for VTables, like this:

1int TallEnemyDraw(){};
2int ShortEnemyDraw(){};
3 
4struct SEnemy
5{
6 int type;
7};
8 
9#define ARR(a, b, c) const int b = a; const (EN ## a*)() = c;
10 
11ARR(1, TallEnemy, TallEnemyDraw)
12ARR(2, ShortEnemy, ShortEnemyDraw)
13 
14#ifndef EN1
15#define EN1 0
16#endif
17 
18#ifndef EN2
19#define EN2 0
20#endif
21 
22#ifndef EN3
23#define EN3 0
24#endif
25 
26#ifndef EN4
27#define EN4 0
28#endif
29 
30const (arr*)()[] = { EN1, EN2, EN3, EN4};
31 
32int DrawAll()
33{
34 SEnemy enemies[2];
35 enemies[0].type = TallEnemy;
36 enemies[1].type = ShortEnemy;
37 for(int ii = 0; ii < 2; ii++)
38 {
39 arr[enemies[ii].type]();
40 }
41}

(Syntax might be a little off, I did not try to compile that)

The idea behind the above code is that I assign an index of the function to a constant simultaneously and in a flexible fashion: i.e. so it would be easy for me to add more enemy types. I also wanted to do this at compile time so I wouldn't have to have an Init() function to fill in my array.

In the end I figured that it is better to just store the function pointers in the SEnemy struct itself. A little less headache that way.

EDIT:
In retrospect I could have used _LINE_ to increment the index automatically... oh well.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

TeamTerradactyl
Member #7,733
September 2006
avatar

Is there a reason you don't want to simply use an enum?

enum EnemyTypes
{
  arr1 = 1, arr2 = 2, ...
} EnemyTypes;

Then once you add more enemies, update EnemyTypes with your new types. enum is even generous enough that you can define them to be non-linear:

enum EnemyTypes
{
  arr1 = 1, arr2 = 15, arr3 = 17;
} EnemyTypes;

It may not be what you want, but I don't understand exactly why you want a const like that; just #define it if you have to, instead of using "const int ..."

SiegeLord
Member #7,827
October 2006
avatar

That's what I originally had. However, to create the VTables I needed to have an init function to fill it out with functions:

enum
{
    TallEnemy,
    ShortEnemy
};

int (fns*)()[MAX_TYPES];

void Init()
{
    fn[TallEnemy] = Fn1;
    fn[ShortEnemy] = Fn2;
}

But I did not like that, as it sort of implied that the array of functions was somehow dynamically alterable... Therefore I wished for it to be const, and defined at compile time.

I have since abandoned that approach, VTables are too annoying to reimplement and I gain no advantage to not doing them the C++ way.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

I gain no advantage to not doing them the C++ way.

Pretty much. If you use C++, you might as well just use C++ features.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Arthur Kalliokoski
Second in Command
February 2005
avatar

If the variables are in some sort of mathmatical relationship (so they can be computed) you could make a little console proglet to write your variables to a file, then paste the result into your source.

like:
fprintf(textfile," %c = [%d],\n",varchar,value);

EDIT
Makes for a horrid makefile, though

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Go to: