Struct Array results in freezing particle

Hey,
I am using a struct array to store some of my data. In setup() i am filling the whole array with default values. It compiles fine but the particle E-Series is blinking red instantly.

struct data_storage
{
    String System_status;
    unsigned long timestamp;
    float flow;
    float flow_bypass;
    float volume;
    int rate_update;
    float temp;
    float p1;
    float U_turbine;
    float U_battery;
} data_storage_unit[10];

void setup(){
clear_data(data_storage_unit, sizeof(data_storage_unit));
}
void loop(){}


bool clear_data(data_storage input_data[], int array_size)
{
    for (int i = 0; i < array_size; i++)
    {
        input_data[i].System_status = "";
        input_data[i].flow = 0;
        input_data[i].flow_bypass = 0;
        input_data[i].volume = 0;
        input_data[i].p1 = 0;
        input_data[i].rate_update = 0;
        input_data[i].temp = 0;
        input_data[i].timestamp = 0;
        input_data[i].U_battery = 0;
        input_data[i].U_turbine = 0;
    }
    return true;
    Log.info("data cleared");
}

Any idea?
Thanks

It would always be a good idea to also state what SOS + x code it blinks :wink:

However, since you are passing in sizeof(array) which is the number of bytes your array occupies and not the number of elements in the array you’ll probably run into an SOS+1 hard fault.
The array size can be calculated like this

const int arrSize = sizeof(elements) / sizeof(elements[0]);

A simple way to debug such issues before consulting the community would be to use Serial.print() statements to check the values against your expectations.
If you printed out array_size inside your clear_data() function you would have seen that it will not hold the expected value of 10 but something much bigger consequently explaining why that code is bound to crash :wink:

BTW, if you intend to store these elements (e.g. in EEPROM) you must make sure to store the String field separately as its content will not be part of the array element. The array element will only hold a pointer to the object.
I’d rather suggest using standard C string (aka char array) as it will make things somewhat easier and potentially more stable too.

1 Like