Should the complier notice this?

Should the complier notice this?

Post by J Robert Ra » Thu, 08 Nov 2001 21:14:40



I spent far too long scratching my head over the following code giving
unexpected results:

DateSecondsToDate(TimGetSeconds() + 24 * 3600, &tomorrow);

Instead of getting 'tomorrow' filled out with the expected values, it
was getting filled out with the same day as today.

Okay so 24 * 3600 is overflowing a 16-bit integer.  Changing the
expression to 'TimGetSeconds() + 86400' cures my problem.

But couldn't the compiler at least warn about this overflow?  Or
better yet, not overflow!

I get the overflow even when I have optimizations on max, does this
mean that the complier is not optimizing away the multiplication?

- Robert

 
 
 

Should the complier notice this?

Post by MWRo » Fri, 09 Nov 2001 00:00:31




Quote:>But couldn't the compiler at least warn about this overflow?  Or
>better yet, not overflow!

Anything is possible but I don't recall of any compiler that does.  I'll
add it as a suggestion.   Sometimes C gives you the power to shoot
yourself in the foot. :)

Ron

--
She was the pride of the American side
When the "gales of November" came early.
Edmund Fitzgerald June 7-1958 November 10-1975
http://www.nauticalworks.com/fitz/

METROWERKS                   Ron Liechty


 
 
 

Should the complier notice this?

Post by Ben Combe » Fri, 09 Nov 2001 01:17:06




Quote:> I spent far too long scratching my head over the following code giving
> unexpected results:

> DateSecondsToDate(TimGetSeconds() + 24 * 3600, &tomorrow);

> Instead of getting 'tomorrow' filled out with the expected values, it
> was getting filled out with the same day as today.

> Okay so 24 * 3600 is overflowing a 16-bit integer.  Changing the
> expression to 'TimGetSeconds() + 86400' cures my problem.

You could have also changed it to

TimGetSeconds() + 24L * 3600L

the L suffix tells the compiler to treat the number as long.

Quote:> I get the overflow even when I have optimizations on max, does this
> mean that the complier is not optimizing away the multiplication?

No, the compiler is optimizing away the multiplication, but it has to
optimize as if the code was executing in the target -- it does 16-bit
math to get the number to substitute.
 
 
 

1. *NOTICE* HP Scanner Drivers *NOTICE*

As the release of Windows 2000 is getting close... keep in mind... HP does
not provide updated drivers without a fee.

The Windows 98 drivers for their scanner go for $25.00 plus shipping.

If you're buying a scanner for Christmas... keep in mind you will be buying
drivers from HP if you upgrade your operating system... shortly after the
holidays.

JR

2. ZOC 1.20 delayed due to hack attempt

3. What I Noticed (FINAL NOTICE)...

4. Psion 5 to 5mx Upgrade...

5. Complier for NeXTstation running NS 3.2

6. The Ultimate Threat, to brute force a perfect symmetric cypher

7. Excel complier

8. TAPI conflict when installing pcAnywhere

9. a complier for excel 5

10. Microprocessor with C Complier

11. imagecraft hc11 c complier

12. = Looking for Pic 16c84 basic complier =

13. i80860 complier needed