ESP32 - Comparing two size_t numbers doesn't work
05:13 09 Feb 2026

I am currently working on ESP32-CAM with the Arduinon IDE latest version.

My task is running on Core 0, to optimize the use of CPUs.

My code, consisting in browsing the pixels of an image seems very simple but I observe a very strange behavior - to make it clearer I copied twice the exact same code and both times I don't get the same result, and the test of comparing the size_t seems not working.

The second loop is never ended

Here is the code :

  const size_t PIXELS_NUMBER = frame->width * frame->height;
  
  Serial.printf ("first a loop of %lu elements\n", (uint64_t) PIXELS_NUMBER);
  size_t bufferIndex3 = 0;
  while (bufferIndex3 < PIXELS_NUMBER ){    
    uint16_t x = (uint64_t) (bufferIndex3 % frame->width);                   // x position in image
    uint16_t y = (uint64_t) (bufferIndex3 / frame->width); // y position in image
    bool test = (bufferIndex3 < PIXELS_NUMBER );
    if (((y % 100) == 0) && (x == 0))  Serial.printf ("Line %u, spot x: %u, y:%u, index : %lu, final: %lu, test : %d\n", y, xloc, yloc, bufferIndex3, PIXELS_NUMBER, test);    
    ++bufferIndex3;
  };

  Serial.printf ("second a loop of %lu elements\n", (uint64_t) PIXELS_NUMBER);
  size_t bufferIndex2 = 0;
  while (bufferIndex2 < PIXELS_NUMBER ){    
    uint16_t x = (uint64_t) (bufferIndex2 % frame->width);                   // x position in image
    uint16_t y = (uint64_t) (bufferIndex2 / frame->width); // y position in image
    bool test = (bufferIndex2 < PIXELS_NUMBER );
    if (((y % 100) == 0) && (x == 0))  Serial.printf ("Line %u, spot x: %u, y:%u, index : %lu, final: %lu, test : %d\n", y, xloc, yloc, bufferIndex2, PIXELS_NUMBER, test);    
    ++bufferIndex2;
  };

And here is the unexpected result - the first loop seems working, but the test of comparing both size_t (the index and in the max) is always true - and the second loop never ended despite the while test ...:

09:17:43.026 -> first a loop of 307200 elements
09:17:43.026 -> Line 0, spot x: 0, y:0, index : 0, final: 307200, test : 1
09:17:43.059 -> Line 100, spot x: 0, y:0, index : 64000, final: 307200, test : 1
09:17:43.093 -> Line 200, spot x: 0, y:0, index : 128000, final: 307200, test : 1
09:17:43.126 -> Line 300, spot x: 0, y:0, index : 192000, final: 307200, test : 1
09:17:43.159 -> Line 400, spot x: 0, y:0, index : 256000, final: 307200, test : 1
09:17:43.159 -> second a loop of 307200 elements
09:17:43.159 -> Line 0, spot x: 0, y:0, index : 0, final: 307200, test : 1
09:17:43.192 -> Line 100, spot x: 0, y:0, index : 64000, final: 307200, test : 1
09:17:43.192 -> Line 200, spot x: 0, y:0, index : 128000, final: 307200, test : 1
09:17:43.225 -> Line 300, spot x: 0, y:0, index : 192000, final: 307200, test : 1
09:17:43.257 -> Line 400, spot x: 0, y:0, index : 256000, final: 307200, test : 1
09:17:43.257 -> Line 500, spot x: 0, y:0, index : 320000, final: 307200, test : 1
09:17:43.289 -> Line 600, spot x: 0, y:0, index : 384000, final: 307200, test : 1
09:17:43.322 -> Line 700, spot x: 0, y:0, index : 448000, final: 307200, test : 1
09:17:43.354 -> Line 800, spot x: 0, y:0, index : 512000, final: 307200, test : 1
09:17:43.354 -> Line 900, spot x: 0, y:0, index : 576000, final: 307200, test : 1
09:17:43.387 -> Line 1000, spot x: 0, y:0, index : 640000, final: 307200, test : 1
09:17:43.419 -> Line 1100, spot x: 0, y:0, index : 704000, final: 307200, test : 1
09:17:43.453 -> Line 1200, spot x: 0, y:0, index : 768000, final: 307200, test : 1
09:17:43.453 -> Line 1300, spot x: 0, y:0, index : 832000, final: 307200, test : 1
09:17:43.486 -> Line 1400, spot x: 0, y:0, index : 896000, final: 307200, test : 1
c++