[Solved]Best practices : lot of struct with array struct* or N array struct

Hello,

What are the best practices for N element known in performance, memory management, ...

struct str s1={...};
..
struct str sn={...};

struct str * str_ref_array={ &s1, .. , &sn};
int N = sizeof(str_ref_array) / sizeof(struct str *);


void f(struct str ** p){
    for(int i=0; i<N; i++){
       ..
    }
}
..
void loop(){
    f (str_ref_array, n);
}

OR

#define N 1000;// N = 1000 for example

struct str  str_array[N];

void f(struct str *p){
    for(int i=0; i<N; i++){
       ..
    }
}

void init(){
    str_array[0]={..};
    ..
    str_array[N-1]={..};
}

void loop(){
    f (str_array);
}

If you allocate each structure separately then you need one pointer to refer to them in the array. So you use 2N bytes or 4N bytes depending on your arduino that might be not necessary if this is just to have the pointers in the array afterwards.

Just allocate directly an array of struct

 struct strArray[] = {
  {…}, // str1
  {…}, // str2
  …
  {…}  // strN
};

PS: N=1000 is likely waaayyy too big if you are on a uno

Thanks ! :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.