Initialize static array in C++

17,881

Solution 1

You need to define it in (exactly)one of your cpp files:

bool KernelFS::available[] = {0};

Solution 2

The problem is that the array is never allocated any memory. You should add this in the global scope:

bool KernelFS::available[] = {false};

Make sure you do it in one cpp file. Adding it in more than one cpp file will result in duplicate symbol error during linking. Also, you shouldn't do this in a header file. The best approach is to add this in the cpp file with the implementation of the KernelFS class.

Note also that static initialization order across compilation units isn't guaranteed. Hence you shouldn't call KernelFS::mount() from static initialization code in another file.

Share:
17,881
vtomic85
Author by

vtomic85

Software engineer, photographer, chess player, keyboards player... wanna learn this, wanna learn that... So many interests, so little time.

Updated on June 04, 2022

Comments

  • vtomic85
    vtomic85 almost 2 years

    I have these classes:

    class FS{
      static char mount(Partition* p)
          {return myImpl->mount(p);}
      /*...*/
      KernelFS* myImpl;
    };
    
    class KernelFS{
    char mount(Partition* p){
       /*...*/
       while(available[i]) i++;
    }
      /*...*/
      static bool available[26];
    };
    

    Main program only uses static functions from FS, e.g:

    void main(){
      Partition* p=/*...*/;
      FS::mount(p);
      /*...*/
    }
    

    When FS::mount(p) is called, it calls myImpl->mount(p) (which is a function from KernelFS class). And here's the problem. When it comes to

    while(available[i]) i++;
    

    ...it breaks! I think the problem is that I haven't initialized the array available[26], and I have no idea how to do that... What else can be the problem? Please help.

    Btw, main() never creates FS or KernelFS objects, so I think that there is no use of constructors...

  • Ulterior
    Ulterior over 12 years
    always looked ugly, but thats it
  • zdd
    zdd almost 11 years
    What if the initial values are not same? Can I use a loop?
  • Alok Save
    Alok Save almost 11 years
    @zdd: Well it will be really intuitive but if you really want to get it towork, here is an implementation.
  • zdd
    zdd almost 11 years
    @Alok Save, I look at the code again, it's not an static array, even not a array in a class, can you double check it?
  • Alok Save
    Alok Save almost 11 years
    @zdd: have you checked using the quirk i said for static array case? Theres no difference.
  • zdd
    zdd almost 11 years
    @Alok Save, as said, that's initialize all elements of a static array to the same value, what if I want different init values? for example, array[0] = 1, array[1] = 2, ..., I don't want to list all the elements one by one. so I need a loop(for or while), but it seems there is no way to use a loop outside a function.
  • Alok Save
    Alok Save almost 11 years
    @zdd: I understood what your requirement is when you posted your first comment. What I said in my previous comment is the code example I showed can be easily modified to make array a static class member. Here i did it for you.