[cc65] Buggy code (was: testsuite updated)

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2003-11-20 17:04:07


On Thu, Nov 20, 2003 at 02:19:04PM +0100, carlos wrote:
>     the randomnumber generator which i sent to the list some days ago,
> output 16bit numbers instead of 32
>     bit ones. That seems to result from a missing "int" in the expression
>     "long unsigned"  at the top of the function genrand(). It seems to be
> intepreted just as "unsigned int".

Have you really tried to change the declaration? If so, you would have noticed
that the change doesn't have any effect.

Since you are using DJGPP (which is actually a special version of gcc), why
not have a look at the warnings it generates? This is what gcc says when
compiling your sources:

----------------------------------------------------------------------------
gcc -Wall -O2 -o mt19937int mt19937int.c
mt19937int.c:101: warning: return type defaults to `int'
mt19937int.c: In function `main':
mt19937int.c:106: warning: unsigned int format, long unsigned int arg (arg 2)
mt19937int.c:110: warning: control reaches end of non-void function
----------------------------------------------------------------------------

It is a good idea to fix compiler warnings instead of ignoring them.
Especially when there seem to be problems with a given source. In your case,
the solution for your problem is exactly outlined in the warnings shown above.
On the i86 platform, "long" and "int" have the same size (32 bit). This is not
true for cc65, where an "int" has 16 bit, while a "long" has 32. So a wrong
format specifier in printf will work on one platform but not on the other. If
you fix the format specifier in line 106 of your sources, you will notice that
the sources start working when compiled with cc65.

>     Is this something, which different compilers treat differently? Is this
> the reason that you never should
>     write just "unsigned" without "int"?

I don't know who made up this rule, but it's plain wrong. "unsigned" and
"unsigned int" are the same type, and the C standard explicitly allows both.
The same is true for "long unsigned", "unsigned long", "long unsigned int" and
"unsigned long int".

>     Just want to tell about this. Please don't think i want you to track
> bugs of other people.
>     It's just, that you can't think about it, if i don't inform you about
> these things.

One side is that I'm glad when people send me comments and bug reports. On the
other side, having to look at sources that are obviously buggy needs time I
could have spend better. In your case it is more the latter than the former.
Please don't get me wrong: Any reports are still welcome, especially when
programs do not seem to compile correctly with cc65. Often enough this is a
problem with cc65. But you can save me quite some work if you double check
that the problematic sources are portable and don't have obvious errors. The
latter includes compiling on all platforms with warnings enabled *and* fix the
code that produces the warnings.

You can even make a more general rule out of this: Always use

        -Wall -Werror

when compiling with gcc.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2003-11-20 17:05:12 CET