Search code examples
arraysarduinoglobalarduino-c++

Using global multidimensional arrays


I'm currently working on an Atmega2560-based hardware project, and I've written about 2000 lines of C++ code - which is now getting unmanageable. It's a project for a local community, and maintainability is important, so I'm trying to pull everything out into modules and keep things simple, so that someone with even less understanding of C++ that I have could at least understand what's going on.

At the heart of the project is a global multidimensional integer array, initially declared in main.cpp (where it worked fine with all functions contained in main.cpp):

// main.cpp
const int A = 30;
const int B = 20;
const int C = 10;
int gArray[A][B][C];

If I do the same in GLOBAL.h, it compiles fine - so, clearly, A, B and C are accepted as constants. But it doesn't work across the modules. If in a code module I use:

// setup.cpp
extern const int A;
extern const int B;
extern const int C;

extern int gArray[A][B][C];

I get the "array bound is not an integer constant before ']' token" error.

So my question is basically - how can I use a global multidimensional array across several modules with the fixed dimensions set in a single place for maintainability?

I've been trying for some to sort this, reading and trying lots of ideas I've read, but I've not been able to make any progress. In the meantime I've created functions to set and to read array values and placed them in GLOBALS.h - which seems inelegant, but is an understandable and practical workaround:

const int A=30;
const int B=20;
const int C=10;

int gARRAY[A][B][C];

void setARRAY(int A, int B, int C, int V) {
    gARRAY[A][B][C] = V;   
}

int getARRAY(int A, int B, int C) {    
    return gARRAY[A][B][C];
}

Solution

  • SOLVED!!!

    First of all, mea culpa - in following the suggestions using gArray[30][20][10], I hadn't noticed that I'd exceeded the RAM capacity!! So, when I might have had what should have been a working method, the processor was crashing [no compiler warnings]. The reality is that I actually only need gArray[10][13][10]

    The result of fixing this and using the tips from the kind comments made it work.

    In case anyone else needs to do use global multidimensional arrays, here's how I did it [note that I'm using the Arduino environment, so there's no int main() for instance]:

    First, in GLOBALS.h I set the dimensions #defines and then declared the array in GLOBALS.cpp:

    // --- GLOBALS.h -------------------------
    #ifndef GLOBALS_H
    #define GLOBALS_H
    
        #define A 10
        #define B 13
        #define C 10
    
    #endif
    
    // --- GLOBALS.cpp -----------------------------
    #include "GLOBALS.h"
    
    int gArray[A][B][C];
    

    I then created a couple of test modules setup.h & setup.cpp to initialise array values and testaccess.h & testaccess.cpp to change the values, as well as main.cpp to test it out [containing the Arduino void setup() and void loop(), the latter isn't shown as it's empty]:

    //--- setup.h ---------------
    void loadArray();
    
    //--- setup.cpp -------------
    #include "GLOBALS.h"
    
    extern int gArray[A][B][C];
    
    void loadArray() {
    
        // set up some arbitrary test values
        gArray[1][1][1] = 99;
        gArray[1][13][1] = 13;
    }
    

    and in testaccess.h & testaccess.cpp I modified the two test values:

    // --- testaccess.h -------------------------
    int testArray();
    
    // --- testaccess.cpp -----------------------
    #include "GLOBALS.h"
    
    extern int gArray[A][B][C];
    
    void testArray() {
        gArray[1][1][1] = 9900;
        gArray[1][13][1] = 1300;
    }
    

    Finally, in my main code I did the testing:

    
    #include <Arduino.h>
    #include "GLOBALS.h"
    #include "setup.h"
    #include "testaccess.h"
    
    
    extern int gArray[A][B][C];
    
    void setup() {
      Serial.begin(9600);
      // set the test values
      loadArray();
      
      // and print them to the serial port to view
      Serial.println(gArray[1][1][1]);   //should be 99
      Serial.println(gArray[1][13][1]);  //should be 13
      Serial.println("----------");
    
      // now modify one of the values and print it:
      gArray[1][13][1] += 10;
      Serial.println(gArray[1][13][1]);  // should be 23
      Serial.println("----------");
    
      // and now modify both values in a different module
      testArray();
      Serial.println(gArray[1][1][1]);   // should be 9900
      Serial.println(gArray[1][13][1]);  // should be 1300
    }
    

    ... and the result on the serial port was ...

    99        
    13        
    ----------
    23        
    ----------
    9900      
    1300  
    

    After two weeks of struggling, I can now move on!

    Thanks all for the help and inspiration.