accessing an array element of an struct array

Hi all,
I am modifying a working code to include several relevant elements to a structure which makes more sense and will be useful for a new feature.
My original code I defined an array:

float var[size_A][size_B] = {};

and I accessed my array element like this:

var[i][j] = 0.0;

In my new code I defined the struct:

struct myStruct{
...
    float    var[size_B];
...
};
myStruct MyStruct[size_A];

and I tried to access the element like this:

MyStruct[i].var[j] = 0.0;

this new code compiles but I had some strange error . Is this way of accessing the array element correct?
thanks.
Yang

soleilsword:
but I had some strange error

Sharing your full code and explaining what the "strange error" is would be conducive to getting a quality answer.

there was another variable that seems to have nothing to do with this structure changed to a strange value during the runtime.

I searched somewhere in the forum and saw someone saying there could be a memory leak due to accessing outside the range of an array, etc.

I'm at least trying to make sure that this code looks okay since I only had that error by changing the 2-d array to struct.

gfvalvo:
Sharing your full code and explaining what the "strange error" is would be conducive to getting a quality answer.

I'm not sure if there is such thing, but I suspect it is because the memory space for my struct array is not allocated.
I tried a few methods to initialize my struct array and once I have that array inside the structure, there seems to be some memory issue. That for instance, I had another integer which was initialized as zero, and it suddently changed to 8224.

OK, since you're not providing code, I guess you have it fixed.

there is 1000 lines of code, I don't know an good way of providing the code.

Make an MRE.

constexpr int size_A = 10;
constexpr int size_B = 10;

struct myStruct{
    float    var[size_B];
};
myStruct MyStruct[size_A];

void setup() {
  // put your setup code here, to run once:
  MyStruct[0].var[0] = 0.0f;
}

void loop() {
  // put your main code here, to run repeatedly:

}

compiled on Arduino 1.8.9

Here is the MRE

constexpr int size_A = 10;
constexpr int size_B = 10;

struct myStruct{
    int       another_var;
    float    var[size_B];
    byte     yet_another_var;
};
myStruct MyStruct[size_A]={};

unsigned long totally_irrelevant_var = 0;

void setup() {
  // put your setup code here, to run once:
  MyStruct[0].var[0] = 0.0f;
}

void loop() {
  // put your main code here, to run repeatedly:

Serial.print(" value = "); Serial.println(totally_irrelevant_var );

}

it certainly compiles and the part of code related to the struct are fine. but the totally_irrelevant_var got refreshed to 8224 while I never aked it to do so.

I had the feeling my implementation is OKAY but rather an arduino issue.

I'm using an arduino zero and I was using SAMD version 1.6.19, when I switched to 1.8.x my issue is gone, but I ended having some other issues which works perfectly in 1.6.x versions.

constexpr int size_A = 10;
constexpr int size_B = 10;

struct myStruct
{
    float    var[size_B];
    byte     yet_another_var;
};
myStruct MyStruct[size_A]={};

unsigned long totally_irrelevant_var = 0;

void setup()
{
  Serial.begin(115200);
  
  MyStruct[0].var[0] = 1.0;
  MyStruct[1].var[1] = 5.4;
}

void loop()
{
  Serial.print("value1 = "); Serial.println(MyStruct[0].var[0]);
  Serial.print("value2 = "); Serial.println(MyStruct[1].var[1]);
  delay(500);
}

^ Works like a charm for me. The concepts and code you posted work and have no bugs. This leads me to believe the problem is more closely tied to an overly high memory usage. Can you post your entire code as an attachment?

Power_Broker:
This leads me to believe the problem is more closely tied to an overly high memory usage.

Not if the MRE exhibits the same bad behavior.

gfvalvo:
Not if the MRE exhibits the same bad behavior.

The MRE works for me, too, so OP is doing something else wrong in the original sketch...

constexpr int size_A = 10;
constexpr int size_B = 10;

struct myStruct{
    int       another_var;
    float    var[size_B];
    byte     yet_another_var;
};
myStruct MyStruct[size_A]={};

unsigned long totally_irrelevant_var = 0;

void setup() {
  Serial.begin(115200); //had to add this
  // put your setup code here, to run once:
  MyStruct[0].var[0] = 0.0f;
}

void loop() {
  // put your main code here, to run repeatedly:

Serial.print(" value = "); Serial.println(totally_irrelevant_var );

}

I'm working on to post the full code. I also think there is something else going on the prevent the code working correctly.