E
Eric Sosman
John said:Eric Sosman said:John said::
John F wrote:
[SNIP]
Use long instead.
why not just:
const long SEC_IN_YEAR=365*24*60*60;
Because if 31536000 does not fit in an int, this
will initialize SEC_IN_YEAR to the wrong value. (If
it manages to initialize SEC_IN_YEAR at all, that is:
this might be undefined behavior.)
Are you sure about this?
Yes. All three multiplications follow the rules for `int'
arithmetic, after which the result is converted to `long'. If
the product is too large for `int', Evil Things will happen.
I can see it now. I usually do not encounter it on my 32 bit targets
where both long and int are 32 bits wide.
That's why I was not so sure at first. But you are right it is a
matter of portability.
Strange how we can spot a problem in one context and then
completely miss the same flaw in another. You were the first
to mention that 31536000 might be too large for an `int',
suggesting you were well aware of the possiblity of an `int'
with fewer than ((runs a few logarithms)) 26 bits. And yet
you got tripped up by long experience with 32-bit `int' ...
Somewhere there's somebody working on a PhD thesis about
how to design programming languages/frameworks/what-have-you
so such oversights are harder to commit. Alas, I suspect the
PhD candidate is probably steeped in computer science when he
really ought to have a background in cognitive psychology ...
Meanwhile, I'll just keep kicking myself in the rear end
whenever I find I've made such a blunder. Unfortunately, it's
becoming uncomfortable to sit ...