0

I made a custom library (let's call it DigitalOutput.h) to learn a bit about librairies and to simplify my work. Here is the basic constructor in the .h file:

DigitalOutput(uint8_t pin);

Here is the code in the .cpp file :

DigitalOutput::DigitalOutput(uint8_t pin)
{
    _pin    = pin;
    _status = LOW;
    pinMode(pin, OUTPUT);
}

So very simple stuff. Now, in my program, I call the function the usual way:

DigitalOutput LED(7); // pin #7 will be configured 
                      // as an output pin to control the LED

Now the issue comes when I create an array and then use the sizeof() function.
Here is the code from my program (using an Arduino Mega):

DigitalOutput   LED_array[] = {22,23,24,25,26,27,28,29,
                               30,31,32,33,34,35,36,37};

So, the array contains 16 elements. But when I do for instance...

#define         LED_NUMBER    sizeof(LED_array)
//...
void setup(){
    //...
    Serial.println(sizeof(LED_array));
    Serial.println(LE_NUMBER);
}

...I get 160 as a result in both cases instead of 16...

So I end up doing a weird #define LED_NUMBER sizeof(LED_array) / 10 in order to get the proper result which is then used throughout the program. Any idea of why this isn't working right?

2
  • Please give a full working example that we can test ourself. Then we will tell you why a single object uses 10 bytes. Despite the compiler optimizations, I think that when the same object is declared more than once, that the size will be the same for every object. – Jot Jun 14 '17 at 10:44
  • 1
    As you already have the answer, I'll just mention a macro to address this very issue, #define countof(a) (sizeof(a)/sizeof(a[0])), where countof(LED_Array) evaluates to the number of members in the array, not to its memory size. I keep this definition in an 'h' file on my local system (named, oddly enough, local.h!) with other simple-but-useful tools I need on occasion. – JRobert Jun 14 '17 at 16:55
4

It is working perfectly correctly.

sizeof doesn't return the number of elements in an array, it returns the number of bytes the array takes up. Since each entry in your array is an instance of your DigitalOutput class, the size is the number of elements multiplied by the size of the class - in this case 10 bytes.

To get the number of elements you must divide the number of bytes by the size of one element:

int numEles = sizeof(LED_array) / sizeof(DigitalOutput);

or

int numEles = sizeof(LED_array) / sizeof(LED_array[0]);
8
  • Well thanks to you all. This was the problem, I did not understand sizeof() well enough and read its definition in the Arduino documentations without really understanding what it was doing. I went back to it with your comments and now I'm getting it. Thanks for your help guys. – Manitoba Jun 14 '17 at 10:57
  • 1
    Note that the first is more robust (using sizeof(DigitalOutput)), in case you ever make LED_array dynamic and it has zero elements. – Michel Keijzers Jun 14 '17 at 11:08
  • 2
    If you make the array dynamic you won't be using sizeof. That will just return the size of the pointer (2 or 4 depending on architecture) regardless of the size of the array. sizeof is a compile-time command not a library function. – Majenko Jun 14 '17 at 11:11
  • Thanks Michel, I actually went with sizeof(DigitalOutput)but without foreseeing the issues you were mentioning. Good to know! Thanks Majenko, that's some good input I actually totally assumed it was a function. – Manitoba Jun 14 '17 at 11:39
  • Most people do. The C++ reference actually lists it as an operator - that is, in the same class as +, /, etc. – Majenko Jun 14 '17 at 11:41
0

You are checking the size of the object (DigitalOutput). This object contains:

  • _pin
  • _status

And this probably results in 10 bytes per object, times 16 objects = 160 bytes.

So you are not creating an (unsigned byte) array which is 16 bytes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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