Go Down

Topic: accessing an array element of an struct array (Read 360 times) previous topic - next topic

soleilsword

Mar 26, 2020, 12:17 am Last Edit: Mar 26, 2020, 12:18 am by soleilsword
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:
Code: [Select]

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

and I accessed my array element like this:
Code: [Select]

var[i][j] = 0.0;

In my new code I defined the struct:
Code: [Select]

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

and I tried to access the element like this:
Code: [Select]

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

gfvalvo

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.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

soleilsword

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.

soleilsword

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.

gfvalvo

OK, since you're not providing code, I guess you have it fixed.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

soleilsword

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

gfvalvo

No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

arduino_new

Code: [Select]

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

soleilsword

Here is the MRE

Code: [Select]


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.

soleilsword

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.

Power_Broker

Code: [Select]
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?
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

gfvalvo

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.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

Power_Broker

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...

Code: [Select]


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 );

}
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

soleilsword

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

Go Up