1

I have created a library that contains an array. This is the .h file:

class Lib {
  public:
    static int arr[];
};

And this is the .cpp file:

#include "Lib.h"

int Lib::arr[] = {0, 1, 2, 3};

When trying to compute the size of this array from within the library, it works just fine. When trying to do this from a sketch, I get the error mentioned in the title. This is the sketch I'm using:

#include <Lib.h>

void setup() {
  sizeof(Lib::arr);
}

void loop() {}

What is the problem here?

4
  • Maybe you can try to add a constant integer inside your library containing the sizeof of the array and in your sketch use that integer. Nov 11, 2019 at 14:48
  • @LukasFin I added an answer. Nov 11, 2019 at 15:01
  • It seems as if the static initializer has not been executed when the sketch is started. That seems strange to me. Did you tried to access the first array element (for example) from within the sketch? I would also use some uncommon values to initialize the array e.g. 53, 33, 4, 12 to be sure you do not access the 0 initialized memory at Lib:arr[0]. If you find the correct value, then at least the initialization works. But then, I have no better explanation. Btw: the correct C++ initialization aof static vars is int Lib::arr[4] = {0, 1, 2, 3};. Perhaps that matters. Nov 11, 2019 at 15:07
  • Sorry, my former comment is nonsense. As this is a compiler error and 'sizeof' is a compiler function that works on declared types, the error is of cause explainable. In the header file the array is not initialized and its size is not specified. The header file is the only thing the compiler knows when compiling the sketch. The cpp file is compiled to an object file in another step. The object file is linked after the compile runs. So all things that happen in the cpp File are unknown to the compiler when it compiles the sketch. If you declare the array with a size it will work. I'm so ... Nov 11, 2019 at 15:54

1 Answer 1

1

The reason is that in your sketch you import only the header file (which is good). However, there it is an array (int[]), not the initialized array.

For that you would need to include the cpp but that is bad practice (if even possible).

So instead, create a new (constant) variable, and assign it.

(more or less pseudo code, due to lack of a compiler):

Lib.h:

class Lib {
  public:
    static int arr[];
    static int arr_length;
};

Lib.cpp:

#include "Lib.h"

int Lib::arr[] = {0, 1, 2, 3};
int Lib::arr_length = sizeof(Lib::arr); 

Sketch:

#include <Lib.h>

void setup() {
  Lib::arr_length;
}


void loop() {}

See also the comment of LukasFun below. It is also possible to set the size of the array in the header file.

My advice though is keeping all assignments in the implementation (cpp) file. The header file is for defining items, the implementation file for initializing/assigning. But it works too, to initialize items in the header file.

2
  • 1
    I just tried setting the size of the array directly in the .h file. If I do this, the sketch compiles without any errors. Maybe you could add this to your answer.
    – LukasFun
    Nov 11, 2019 at 15:05
  • @LukasFun True, I added the comment, however, it's still better to do initializations in the implementation file. Nov 11, 2019 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.