Compile-time overflow checks

bearophile bearophileHUGS at lycos.com
Sat Sep 5 10:19:35 PDT 2009


This comes from a post from D.learn:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=17520

This line of code:
const ulong N = 1024 * 1024 * 1024 * 2;

The programmer has not seen N to be 2_147_483_648UL but 18_446_744_071_562_067_968UL.

Steven Schveighoffer has given a good explanation, saying that's "expected and well-defined behavior".
In truth that's a bug in the code caused by wrongly defined C language specs.
Still, C programmers may expect such weird and unnatural behaviour from the D compiler too...

D isn't forced to follow C specs. So to solve this bad situation compile-time overflow checks can be added to D, able to show an overflow warning at compile-time in such line of code. Such warning can be disabled locally or globally by people that love the standard C behaviour.

Such compile-time checks give zero penalty at run-time.

Bye,
bearophile



More information about the Digitalmars-d mailing list