[NTLUG:Discuss] Thinking ALPHA
Richard Cobbe
cobbe at directlink.net
Sun Sep 26 10:18:02 CDT 1999
cbbrowne at godel.brownes.org wrote on 9-26-1999:
> On Sat, 25 Sep 1999 09:25:35 CDT, the world broke into rejoicing as
> Steve Baker <sjbaker1 at airmail.net> said:
> > The biggest problem area seemed to be related to doing floating point
> > math on undefined variables - even in situations where the results
> > don't matter:
> >
> > Here is a much simplified example:
> >
> > float x ; /* Undefined */
> >
> > if ( condition )
> > x = 6.0f ;
> >
> > x += 2.0f ; /* Maybe an operation on an undefined variable */
> >
> > if ( condition )
> > printf ( "%f\n", x ) ; /* But who cares? */
> >
> >
> > This kind of thing would sometimes core dump on the Alpha if
> > condition==FALSE, where x86, PPC, 68000, MIPS, etc CPU's would
> > happily sail on past. Clearly it's sloppy programming on my part
> > - but it's a pain to debug if you don't have an Alpha.
>
> I always found it something of a convenience that Pascal would always
> "puke" on that sort of thing; 'tis quite useful to *know* that you've not
> initialized a variable.
At the risk of re-opening an old flame war, gcc will detect this sort of
thing. You have to compile with optimizations on, though, because it
relies on data-flow analysis that isn't done without optimizations.
Take the following foo.cpp:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
// the following is a lousy condition, but it suffices for this
// example.
if (rand() & 0x01)
{
printf("initing i\n");
i = 3;
}
i += 2;
printf("%d\n", i);
return 0;
}
And a compilation:
[minbar:~]$ g++ -Wall -O2 -o foo foo.cpp
foo.cpp: In function `int main()':
foo.cpp:6: warning: `int i' might be used uninitialized in this function
Actually, the -Wall isn't necessary; a -Wuninitialized will do. Also,
according to the info page, -O, -O1, or -O3 will work fine as well,
although I haven't tested this.
Richard
More information about the Discuss
mailing list