DLyons
DLyons

Reputation: 186

Initializing a constant array using a loop?

I have a constant lookup table which is keyed by fairly sparse "magic numbers"/ enumeration values, so standard {} positional initialization would at best be incredibly tedious.

I've tried initializing it in a separate source file, "constants.cpp"

#define SORT_ARRAY_SIZE 1024
size_t kSortArray[SORT_ARRAY_SIZE];

void InitializeSortArray()
{

    //  Only a subset of the reserved SORT_ARRAY_SIZE positions are used.
    //  Which ones are compile-time "magic numbers".
    for (int i = 0; i < SORT_ARRAY_SIZE; ++i)
    {
        switch (i)
        {
        case 57:  //  Magic number #1.
            kSortArray[i] = 0;
            break;
        case 213:  //  Magic number #2.
            kSortArray[i] = 1;
            break;
        }
    }
}

And then making it external in a header file, constants.h

#ifndef CONSTANTS_H
#define CONSTANTS_H

#define SORT_ARRAY_SIZE 1024
extern size_t kSortArray[SORT_ARRAY_SIZE];
#endif

It's then used in the main file binner.c e.g.

int main(int argc, char* argv[])
{
    kSortArray[0] = 3;  //  Compile-time error desirable.
    return 0;
}

All that compiles and works fine, except that the assignment in main doesn't cause a compile-time error because I haven't yet declared "kSortArray" as a constant anywhere. But when I try including a constant keyword in either of "constants.h / constants.cpp" I get errors. Is this approach doomed to failure?

I tried some other suggestions like putting it in a class and using the constructor function but my attempt threw all sorts of template errors.

Any help much appreciated.

Upvotes: 2

Views: 2501

Answers (1)

Peter
Peter

Reputation: 36617

I suggest doing it like this

//   in the header

//   include whatever header you're using to declare size_t

const size_t SORT_ARRAY_SIZE = 1028;
extern const size_t *kSortArray;

//  in your constants compilation unit


namespace
{
    static size_t the_array[SORT_ARRAY_SIZE];
}

const size_t *kSortArray = ::the_array;

Then change your InitializeSortArray() so it initialises ::the_array.

Upvotes: 1

Related Questions