i have a function to check a timeout and i would like it to take two arguments of size_t (to be sure the biggest value of millis() will fit).
However when declaring the function with size_t arguments the code does no longer work. It compiles but it does nothing.
With arguments wrongPassTimestamp and wrongPassTimeout declared as unsigned long there are no problems.
const unsigned long wrongPassTimeout = 15000L;
unsigned long wrongPassTimestamp = 0L;
bool CheckPasswordTimeout(size_t wrongPassTimestamp, size_t wrongPassTimeout)
{
if (millis() - wrongPassTimestamp < wrongPassTimeout)
return true;
else
return false;
}
Can you please help me find out what am i missing?
This works fine:
bool CheckPasswordTimeout(unsigned long wrongPassTimestamp, unsigned long wrongPassTimeout)
{
if (millis() - wrongPassTimestamp < wrongPassTimeout)
return true;
else
return false;
}
std::size_t can store the maximum size of a theoretically possible object of any type (including array).
The bit width of std::size_t is not less than 16.
So on an Arduino platform, you'll actually have to dig pretty deep to figure out what size_t actually is (16 bit? 32?) and it will be platform-dependent.
Using size_t the way you're doing doesn't make sense.
By definition millis() outputs unsigned long, so you know what data type you need - no need to second-guess, wrap another layer of obfuscation around it etc.
size_t is a type that can hold the maximum size of an object (like an array or struct), and has nothing to do with the size of a particular variable, or the milliseconds values used in Arduino. So on an AVR, the maximum size of an array is 32768 bytes, which fits in a 2-byte "unsigned int", and size_t is equivalent to uint16_t.
millis() in Arduino is defined to return uint32_t (on any platform) (that's 4 bytes, far less than the 32k max size of an object.)
I see many times that others declare a uint32_t variable to be used with millis(). I don't like that, there is no need for it. Its purpose seems to be to make it more interesting as if that makes it better. I think it is not needed.
It matters less what the return type of millis() and micros() are. What matters more are the (unsigned) data types used to store the return value: 8 bit is sufficient for timing <= 255 ms/µs, 16 bit is sufficient for timing <= 65.5 sec/ms and so on.