A function to sleep a 1000 years: explained
bowero.nl
bowero.nl
Overall the thought of a pinned register being software updated once every 7 seconds for near a thousand years is the worst part.
I stand corrected; I'm so used to seeing these unrolled (which hopefully the hypothetical compiler's done) that I didn't consider the stack implications; that's the most cringy part.
At the very least, anything that's intended to last a long time should be written in the simplest, clearest, self-explanatory manner so as to at least give reasonable confidence it's correct.
Not to mention, 952 years is a lousy approximation of a millennium.
const yearSeconds = 31556926
for i = 1 to 1000
sleep yearSeconds
Or, use some calendar library to get the exact number needed.Of course, even without leap seconds, this severely complicates the issue since you'd first need to figure out the number of leap days in the next 1000 years, which is unfortunately not fixed since 1000 is not a multiple of 400.
Edit: I used the tropical year number without thinking about it, but (a) the difference is de minimus compared to being 5% off, and (b) I consider it undefined whether the Gregorian calendar year really is different, because who's to say we won't have an adjustment to the leap year cycle in the very long run to make them converge?
You can try this yourself by compiling:
#include <stdio.h>
int i = 0; int x() {--i && x(printf("x"));} int main() {x();}
using gcc with and without '-02'.
void
f (int i, int j)
{
int k = j;
k <<= 2;
k += j;
k <<= 3;
if (--i)
while (--k)
f(i, j);
else
sleep(k);
}
int
main ()
{
f (3, 'O'); /* Sleep for 1000 years */
return 0;
}
...this should solve your stack overflow problems and be 99.9% accurate. f(i, j, k, l) { for (j = (j << 2) + j << 3; i + 1 && --j; f(i - 1, k, l, l)); sleep(j); }
int
main ()
{
f(1, 'O', 'O', 'O'); /* Sleep for 1000 years */
}I wouldn't say 'correct', exactly. Not just int size, it depends on undefined behavior and a moderate level of compiler optimizations at the same time. Good luck there.
There are a lot of tricks to minimize the character count, and replacing a();b() with b(a()) is one of them.