DigitalOut array bug?

13 Mar 2019

Hello,

I use Mbed LPC1768.

Today i fall on a bug with DigitalOut array when you try to write out of range.

If you initialize an array in first you dont have the same result with an array in last.

Like you can see beelow i had try on lots of compilers and the results change again.

array first

#include "mbed.h"

DigitalOut leds[] = {LED1, LED2};
DigitalOut out(p5);

int main()
{
    leds[-1] = 1;
    leds[0] = 1;
    leds[2] = 1;
    leds[1] = 1;
} 

Mbed OS Simulator -> LED1 and LED2 are ON

Mbed Online Compiler with mbed 2 -> LED1 and LED2 are ON

Mbed Online Compiler with mbedOS 5.9.7 -> LED1 and LED2 are OFF

Mbed CLI Compiler with GCC_ARM and mbedOS 5.9.7-> LED1 and LED2 are OFF

Mbed Studio Compiler with ARMC6 and mbedOS 5.9.7 -> LED1 blink and LED2 OFF

array last

#include "mbed.h"

DigitalOut out(p5);
DigitalOut leds[] = {LED1, LED2};

int main()
{
    leds[-1] = 1;
    leds[0] = 1;
    leds[2] = 1;
    leds[1] = 1;
}

Mbed OS Simulator -> LED1 and LED2 are ON

Mbed Online Compiler with mbed 2 -> LED1 ON and after a little delay LED1 blink and LED2 OFF

Mbed Online Compiler with mbedOS 5.9.7 -> LED1 ON and LED2 OFF

Mbed CLI Compiler with GCC_ARM and mbedOS 5.9.7-> LED1 and LED2 are OFF

Mbed Studio Compiler with ARMC6 and mbedOS 5.9.7 -> LED1 blink and LED2 OFF

I think Mbed CLI Compiler with GCC_ARM or ARMC6 give good results, isn't it? But why these differences...?

Thanks for your help, YSI

13 Mar 2019

Hi YSI

What you are trying to do is a out of boundary bug, as you described. Please note that C++ compilers generally do not catch these.

Therefore, I would recommend you to check that whether those boards crashed, that did not turn the leds on.

Memory location at [-1] or [2] might be just random data, and therefore when DigitalOut is trying to use that data, random things might occur.

This is not a bug that can be handled in OS level, as the static array is a C++ feature and indexes are responsible of application. Please study https://wiki.sei.cmu.edu/confluence/display/c/ARR00-C.+Understand+how+arrays+work

14 Mar 2019

Hi Seppo Takalo,

Thank you for your reply!

I understand what you are saying with a random occurrence when you are out of range in a table.

But in my mind, I had always associated any writing out of range to a crash of the board and I was very confused when the first code ran uninterrupted with the online compiler and mbed lib 2.0.

You need to log in to post a reply