use of sizeof

27 Mar 2011

I tried the LED chase program by Jeff Bosch, posted last year.

I do not understand the use of sizeof(DigitalOut) in his code:

      DigitalOut leds[] = {LED1, LED2, LED3, LED4};
      int numLeds = sizeof(leds)/sizeof (DigitalOut);
      ... more code

To me, it looks like we want numLeds to equal four since our array has four elements. So then I assume that sizeof(leds) returns 4 and sizeof(DigitalOut) returns 1.

But this is clearly not the case. With the division the code works and without it, it does not.

How can I determine what an expression like sizeof(DigitalOut) returns?

26 Mar 2011

the sizeof DigitalOut is probably not 1, it's however many bytes a single DigitalOut occupies in memory.

for example (I'm making up numbers), if a DigitalOut takes up 6 bytes, and here's 4 of them, then sizeof(leds) will be 24, and sizeof(DigitalOut) will be 6, 24/6 = 4, and you have the right number.

you can experiment with just printf("%d", sizeof(DigitalOut)); to find out the value.

26 Mar 2011

Hi,

The sizeof code usage is more like:

int arraysize = sizeof(leds)/sizeof(leds[0]);

I use code like this in my app and confirmed the answer is correct, in your sample 4.

Except off-course when the array has 0 elements. I'm not sure what sizeof(DigitalOut) returns and if it's allowed.

27 Mar 2011

Thank you for your replies. Using OS X 10.6.7 and its Terminal. In Terminal I type in: screen /dev/tty.usbmodem* and Return. This brings up a window which can capture serial sent from mBED through the USB cable. However, if I quit Terminal, this no longer works until I unmount and remount the mBed device. Then I can use Serial as described in the Handbook.

#include "mbed.h" 
DigitalOut leds[] = {LED1, LED2, LED3, LED4};
Serial pc(USBTX, USBRX);
int main() {
    int numLeds = sizeof(leds)/sizeof(DigitalOut);
    pc.printf("%d", sizeof(DigitalOut));
    pc.printf("%d", sizeof(leds));
    pc.printf("%d",numLeds);
}

Interestingly, sizeof(DigitalOut) always return 28 (bytes I'm now assuming). And sizeof(leds) will return 112, 84, 56, or 28, depending on the array, whether 4, 3, 2, or 1 element(s). I'm using %d as suggested in the printf to get decimal.

So numLeds does always come out correct for the LED chasing code, which works for 2, 3, or 4 of the built-in LEDs. The code from Jeff Bosch seems completely general so that it could chase as many external LEDs as one wished to hook up.

#include "mbed.h"
DigitalOut leds[] = {LED1,LED2, LED3, LED4};
int main() {
    int i, previous;
    int numLeds = sizeof(leds)/sizeof(DigitalOut);
    while (1) {
        for (i=0; i<numLeds; i++) {
            if (i==0) previous = (numLeds - 1);
            else previous = (i-1);
            leds[i] = 1;
            leds[previous] = 0;
            wait(0.1);
        }
    }
}
27 Mar 2011

I suspect that screen isn't getting shutdown when you close Terminal only and it is still holding the serial port open. You can exit screen and return back to Terminal by doing the following:

  • Press CTRL+A
  • Press k
  • Press y

This will issue the kill command to screen and then the final y confirms that you want the kill to occur.

The sizeof operator does return the size of its parameter (variable or type) in bytes. 28 bytes per array element makes sense:

class DigitalOut : public Base {    // 16 bytes in base class See below
protected:
    PinName             _pin;       // 4 bytes for enum
    LPC_GPIO_TypeDef    *_gpio;     // 4 bytes for pointer
    uint32_t            _mask;      // 4 bytes for 32-bit value
};

class Base {
public: 
    virtual ~Base();                // 4 bytes for v-table pointer

	protected: 
	    Base *_next;                // 4 bytes for pointer
	    const char *_name;          // 4 bytes for pointer
	    bool _from_construct;       // 4 bytes

};
27 Mar 2011

Adam,

Very useful information. I'm glad to learn more about screen and Terminal; your suggestions work. I'm glad to see the underlying reason for the 28 bytes.

Thank you.