Soucis avec un tableau sous Platformio

Bonjour tout le monde,
Je rencontre un soucis avec un tableau qui est déclaré comme ça:

  // Tableau de données à écrire
  uint8_t data[] = {0x01, 0x02, 0x03, 0x04};

je désire affiché mon tableau avec le code suivant :

Oups, suite de message

       Serial.print("Le tableau :"); Serial.println(sizeof(data));
        for (int i = 0; i < 5; i++)
        {
          Serial.print(data[i]);Serial.print(":");Serial.print(i);
          if (i < 4) Serial.print(", ");
        }
        Serial.println("");

J'obtient :
Le tableau :4
1:0, 2:1, 3:2, 4:3, 10:4, 0:5, 0:6, 0:7, 108:8, 34:9, 251:10, 63:11, 136:12, 129:13, 251:14, 63:15, 1:16, 0:17, 0:18, 128:19, 0:20, 0:21, 0:22, 0:23, 176:24, 34:25, 251:26, 63:27, 0:28, 0:29, 0:30, 0:31, 0:32, 0:33, 0:34, 0:35, 0:36, 0:37, 0:38, 0:39, 0:40, 0:41, 0:42, 0:43, 0:44, 0:45, 0:46, 0:47, 0:48, 0:49, 0:50, 0:51, 0:52, 0:53, 0:54, 0:55, 0:56, 0:57, 0:58, 0:59, 0:60, 0:61, 0:62, 0:63, 0:64, 0:65, 0:66, 0:67, 0:68, 0:69, 0:70, 0:71, 0:72, 0:73, 0:74, 0:75, 188:76, 34:77, 251:78, 63:79, 0:80, 0:81, 0:82, 0:83, 0:84, 0:85, 0:86, 0:87, 0:88, 0:89, 0:90, 0:91, 0:92, 0:93, 0:94, 0:95, 0:96, 0:97, 0:98, 0:99, 0:100, 0:101, 0:102, 0:103, 0:104, 0:105, 0:106, 0:107, 0:108, 0:109, 0:110, 0:111, 0:112,

avec un défilement qui ne s'arrête pas.
Où est mon erreur.

Merci de votre aide
PS: je suis sous W11, VSCode

Et j'ai oublié avec un ESP WROOM 32

Alors, je ne vois pas d'où vient ce comportement, mais déjà ca devrait être i<4 au niveau de la boucle. Tu as 4 valeur d'indice 0, 1, 2, 3

Oui effectivement, mais cela na change pas grand chose.
Voici le code qui fonctionne

void setup() {
  Serial.begin(115200);
  Serial.println();
  uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
  int dataSize = sizeof(data);
  int x = sizeof(data);
  Serial.print("Le tableau :"); Serial.println(sizeof(data));
  for (int i = 0; i < 4; i++)
  {
    Serial.print(data[i]);Serial.print(":");Serial.print(i);
    if (i < 4) Serial.print(", ");
  }
  Serial.println("");
}
void loop() {
}

Avec un Nano j'avais le même comportement mais avec un message d'avertissement
warning: iteration 4 invokes undefined behavior [-Waggressive-loop-optimizations]
et en recherchant j'ai trouvé :
[Solved] Infinite loop without "Warning: iteration N invokes undefined behavior", using pointers to get data from a function

merci pour ta réponse

en réalité le code est le suivant

void setup() {
  Serial.begin(115200);
  Serial.println();
  uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
  int dataSize = sizeof(data);
  int x = sizeof(data);
  Serial.print("Le tableau :"); Serial.println(sizeof(data));
  for (int i = 0; i < dataSize; i++)
  {
    Serial.print(data[i]);Serial.print(":");Serial.print(i);
    if (i < dataSize) Serial.print(", ");
  }
  Serial.println("");
}
void loop() {
}


J'ai également trouvé une explication que je comprends mieux ici:
https://forum.arduino.cc/t/beginner-for-loop-with-warning-waggressive-loop-optimizations/355463/5

Et mon code devient :

void setup() {
  Serial.begin(115200);
  Serial.println();
  uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
  int dataSize = sizeof(data);
  #define nbElement(x)  (sizeof(x))
  // int nbElement = sizeof(data) / sizeof(data[0]);

  int x = sizeof(data);
  Serial.print("Le tableau :"); Serial.println(nbElement(data));
  for (int i = 0; i < nbElement(data); i++)
  {
    Serial.print(data[i]);Serial.print(":");Serial.print(i);
    if (i < nbElement(data)-1) Serial.print(", ");
  }
  Serial.println("");
}
void loop() {
}

Si j'ai bien compris, c'est un pb que l'on peut rencontré si on a :
It's a perfectly understandable mistake, as is the one you'll make if you try to use the same trick inside a function, on function parameter that happens to be a pointer :wink:

Merci à vous

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